No.1 Check if the Sentence Is Pangram
Their thinking
Simply traverse the string to determine whether non-alphanumeric characters return false or record the number of different characters to satisfy the 26 code displays
if (sentence == null || sentence.length() == 0) {
return false;
}
Set<Character> set = new HashSet<>();
for (char c : sentence.toCharArray()) {
if (c - 'a' < 0 || c - 'a' >= 26) {
return false;
}
set.add(c);
}
return set.size() == 26;
}
Copy the code
No.2 Maximum Ice Cream Bars
Their thinking
Greedy to buy ice cream need not be seen as a backpack problem, TLE or MLE code shows
if (costs == null || costs.length == 0) { return 0; } int n = costs.length; int res = 0; // Sort from small to large Arrays. Sort (costs); for (int cost : costs) { if (coins > cost) { res += 1; coins -= cost; } else { return res; } } return res; }Copy the code
No.3 Single-Threaded CPU
Their thinking
MinHeap operations are sorted according to the duration of their execution. If they are the same duration, they are sorted according to index. The time line moves backward until they reach the start time to join the heap. At the same time, take out the task on the top of the heap and start executing, and record the answer code display
public int[] getOrder(int[][] tasks) { int n = tasks.length; Index int[][] newTasks = new int[n][3]; for (int i = 0; i < tasks.length; i++) { newTasks[i][0] = tasks[i][0]; newTasks[i][1] = tasks[i][1]; newTasks[i][2] = i; } Array.sort (newTasks, Comparator.comparingInt(x -> x[0])); PriorityQueue<Integer> minHeap = new PriorityQueue<>((x, y) -> { if (newTasks[x][1] == newTasks[y][1]) { return newTasks[x][2] - newTasks[y][2]; } return newTasks[x][1] - newTasks[y][1]; }); int curTime = 1; int[] res = new int[n]; // task index offer to heap int i = 0; // task index add to result int j = 0; while (i < n) { while (i < n && (minHeap.isEmpty() || curTime >= newTasks[i][0])) { curTime = Math.max(curTime, newTasks[i][0]); minHeap.offer(i++); } // From the heap to execute if (! minHeap.isEmpty()) { int[] task = newTasks[minHeap.poll()]; res[j++] = task[2]; curTime += task[1]; } } while (! minHeap.isEmpty()) { res[j++] = newTasks[minHeap.poll()][2]; } return res; }Copy the code
No.4 Find XOR Sum of All Pairs Bitwise AND
Their thinking
Bit operation derivation, pure mathematics
Front skills: a radius b = (~ a & b) | (a & ~ b)
Therefore: (a & b1) radius (a & b2) = [(~ a | ~ b1) and (a) | b2] | [[(~ a | ~ b2) & (a | b1)]
So :(a & b1) ⊕ (a & b2) = a & (b1 ⊕ b2)
The original (a & b1) radius (a1 and b2) (a1 & b3) radius (a1 & bm)… The radius (an & bm)
Equivalent: [a1 & b1 radius (b2 radius… bm)] radius [a2 & (b1 radius b2 radius… bm)] the radius… ⊕ [an & (b1⊕ B2 ⊕…..bm)]
Bit operation learning welcome advice ashore algorithm one – on – one customized guidance
The code shown
int sumArray2 = 0;
int res = 0;
for (int i : arr2) {
sumArray2 ^= i;
}
for (int i : arr1) {
res ^= (i & sumArray2);
}
return res;
}
Copy the code