Java算法 leetcode简单刷题记录8

Java算法 leetcode简单刷题记录8

  1. 找出不同元素数目差数组: https://leetcode.cn/problems/find-the-distinct-difference-array/

  2. 保龄球游戏的获胜者: https://leetcode.cn/problems/determine-the-winner-of-a-bowling-game/

  3. 计数器II: https://leetcode.cn/problems/counter-ii/

  4. 找出转圈游戏输家: https://leetcode.cn/problems/find-the-losers-of-the-circular-game/

    case过不了的就继续写;

class Solution {
    public int[] circularGameLosers(int n, int k) {
        int[] cnts = new int[n + 1];
        Arrays.fill(cnts, 0);
        int i = 1;
        int t = 1;
        while (true) {
            if (i > n) {
                i = i % n;
                if (i == 0) {
                    i = n;
                }
            }
            cnts[i] += 1;
            System.out.println(t + " " + i);
            if (cnts[i] == 2) {
                break;
            }
            i += t * k;
            t++;
        }
        int cnt = 0;
        for (int j = 1; j < cnts.length; j++) {
            if (cnts[j] == 0) {
                cnt += 1;
            }
        }
        // System.out.println(cnt);
        int[] res = new int[cnt];
        i = 0;
        for (int j = 1; j < cnts.length; j++) {
            if (cnts[j] == 0) {
                res[i] = j;
                i = i + 1;
            }
        }
        return res;
    }
}
  1. 删除子串后的字符串最小长度: https://leetcode.cn/problems/minimum-string-length-after-removing-substrings/
    循环删除子串,可以用递归的replaceAll;
class Solution {
    public int minLength(String s) {
        int n = calcMinLength(s);
        return n;
    }

    public int calcMinLength(String s) {
        int length = s.length();
        String newStr = s.replaceAll("AB", "").replaceAll("CD", "");
        if (length == newStr.length()) {
            return length;
        } else {
            return calcMinLength(newStr);
        }
    }

}

你可能感兴趣的:(JAVA,算法,算法,java,leetcode)