寫的要沒信心了
還是去賣雞排好了...
又一間交易所倒了...
真希望能到穩定的公司養老
寫點EASY的刷刷信心...
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/intersection-of-two-arrays/
class Solution {
public int[] setIntersection(Set<Integer> set1, Set<Integer> set2) {
int [] output = new int[set1.size()];
int idx = 0;
for (Integer s : set1) {
if (set2.contains(s)) {
output[idx++] = s;
}
}
return Arrays.copyOf(output, idx);
}
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<Integer>();
for (Integer n : nums1) {
set1.add(n);
}
Set<Integer> set2 = new HashSet<Integer>();
for (Integer n : nums2) {
set2.add(n);
}
if (set1.size() < set2.size()) {
return setIntersection(set1, set2);
}
return setIntersection(set2, set1);
}
}
Q: https://leetcode.com/problems/house-robber-iii/description/
class Solution {
public int[] helper(TreeNode node) {
if (node == null) {
return new int[] { 0, 0 };
}
int left[] = helper(node.left);
int right[] = helper(node.right);
int rob = node.val + left[1] + right[1];
int notRob = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
return new int[] { rob, notRob };
}
public int rob(TreeNode root) {
int[] answer = helper(root);
return Math.max(answer[0], answer[1]);
}
}
Q: https://leetcode.com/problems/logger-rate-limiter/
class Logger {
Map<String, Integer> timeByMessage = new HashMap<>();
public Logger() {
}
public boolean shouldPrintMessage(int timestamp, String message) {
if (!timeByMessage.containsKey(message)) {
timeByMessage.put(message, timestamp);
return true;
}
int preTime = timeByMessage.get(message);
if (timestamp - preTime < 10) {
return false;
}
timeByMessage.put(message, timestamp);
return true;
}
}
/**
* Your Logger object will be instantiated and called as such:
* Logger obj = new Logger();
* boolean param_1 = obj.shouldPrintMessage(timestamp,message);
*/
Q: https://leetcode.com/problems/water-and-jug-problem/description/
class Solution {
public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
Set<String> stateSet = new HashSet<>();
Queue<int[]> q = new LinkedList<>();
q.offer(new int[] {0, 0});
while(!q.isEmpty()) {
int[] jugs = q.poll();
int water1 = jugs[0];
int water2 = jugs[1];
stateSet.add(water1 + "-" + water2);
if (water1 + water2 == targetCapacity) {
return true;
}
if (jug1Capacity > water1) {
if (!stateSet.contains(jug1Capacity + "-" + water2)) {
q.offer(new int[] {jug1Capacity, water2});
}
if (water1 + water2 < jug1Capacity) {
if (!stateSet.contains(water1 + "-" + water2)) {
q.offer(new int[] {water1 + water2, 0});
}
} else {
if (!stateSet.contains(jug1Capacity + "-" + (water1 + water2 - jug1Capacity))) {
q.offer(new int[] {jug1Capacity, water1 + water2 - jug1Capacity});
}
}
}
if (jug2Capacity > water2) {
if (!stateSet.contains(water1 + "-" + jug2Capacity)) {
q.offer(new int[] {water1, jug2Capacity});
}
if (water1 + water2 < jug2Capacity) {
if (!stateSet.contains(0 + "-" + (water1 + water2))) {
q.offer(new int[] {0, water1 + water2});
}
} else {
if (!stateSet.contains((water1 + water2 - jug2Capacity) + "-" + jug2Capacity)) {
q.offer(new int[] {water1 + water2 - jug2Capacity, jug2Capacity});
}
}
}
if (water1 > 0) {
if (!stateSet.contains(0 + "-" + water2)) {
q.offer(new int[] {0, water2});
}
}
if (water2 > 0) {
if (!stateSet.contains(water1 + "-" + 0)) {
q.offer(new int[] {water1, 0});
}
}
}
return false;
}
}