21號了
先當作30天練練刷題吧
O_Q HR那邊可能發生什麼事情了
我就慢慢等 慢慢等 慢慢等 慢慢等 等到我都睡著了~
Q: https://leetcode.com/problems/backspace-string-compare/description/
class Solution {
public boolean backspaceCompare(String s, String t) {
Stack<Character> st1 = new Stack<>();
Stack<Character> st2 = new Stack<>();
for (int i = 0;i < s.length();i++) {
if (s.charAt(i) == '#' && !st1.isEmpty()) {
st1.pop();
} else if (s.charAt(i) != '#') {
st1.push(s.charAt(i));
}
}
for (int i = 0;i < t.length();i++) {
if (t.charAt(i) == '#' && !st2.isEmpty()) {
st2.pop();
} else if (t.charAt(i) != '#') {
st2.push(t.charAt(i));
}
}
while (!st1.isEmpty() && !st2.isEmpty()) {
if (st1.pop() != st2.pop()) {
return false;
}
}
return st1.isEmpty() && st2.isEmpty();
}
}
Q: https://leetcode.com/problems/repeated-substring-pattern/description/
class Solution {
public boolean repeatedSubstringPattern(String s) {
int n = s.length();
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) {
StringBuilder pattern = new StringBuilder();
for (int j = 0; j < n / i; j++) {
pattern.append(s.substring(0, i));
}
if (s.equals(pattern.toString())) {
return true;
}
}
}
return false;
}
}
Q: https://leetcode.com/problems/koko-eating-bananas/description/
class Solution {
public int minEatingSpeed(int[] piles, int h) {
int min = 1;
int max = Integer.MIN_VALUE;
for (int pile : piles) {
if (pile > max) {
max = pile;
}
}
while (min < max) {
int mid = min + (max - min) / 2;
int useHours = eatingHours(piles, mid);
if (useHours <= h) {
max = mid;
} else {
min = mid + 1;
}
}
return max;
}
private int eatingHours(int[] piles, int k) {
int hour = 0;
for (int pile : piles) {
hour += (int) Math.ceil((double) pile / k);
}
return hour;
}
}
Q: https://leetcode.com/problems/number-of-recent-calls/
class RecentCounter {
Queue<Integer> slideWindow;
public RecentCounter() {
this.slideWindow = new LinkedList<Integer>();
}
public int ping(int t) {
this.slideWindow.offer(t);
while (this.slideWindow.peek() < t - 3000) {
this.slideWindow.poll();
}
return this.slideWindow.size();
}
}