iT邦幫忙

2023 iThome 鐵人賽

DAY 21
0

21號了
先當作30天練練刷題吧
O_Q HR那邊可能發生什麼事情了
我就慢慢等 慢慢等 慢慢等 慢慢等 等到我都睡著了~/images/emoticon/emoticon02.gif

Backspace String Compare

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();
    }
}

Repeated Substring Pattern

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;
    }
}

Koko Eating Bananas

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;
    }
}

Number of Recent Calls

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();
    }
}

上一篇
09/20
下一篇
09/22
系列文
30天準備google面試30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
yale918
iT邦新手 4 級 ‧ 2023-09-21 17:29:47

呼喚 HR小姊姊 ~~

我要留言

立即登入留言