Algorithm: Leetcode Contest 408
Find if Digit Game Can Be Won
Solution
class Solution {
public boolean canAliceWin(int[] nums) {
int sum1 = 0, sum2 = 0;
for(int num: nums) {
if (num < 10) {
sum1 += num;
} else {
sum2 += num;
}
}
return sum1 != sum2;
}
}
easy~
3234. Count the Number of Substrings With Dominant Ones
Solution 1 - Brute Force
class Solution {
public int numberOfSubstrings(String s) {
// brute force, 2 for loop to check all sub strings
char[] cs = s.toCharArray();
int res = 0;
int num1 = 0, num0 = 0;
for(int l = 0; l < cs.length; l++) {
for(int r = l; r < cs.length; r++) {
num1 = 0;
num0 = 0;
for(int i = l; i <= r; i++) {
if (cs[i] == '0') {
num0++;
} else if (cs[i] == '1') {
num1++;
}
}
if (num1 >= Math.pow(num0, 2)) {
res++;
}
}
}
return res;
}
}
actually cannot understand the ACed solution shared by others…