905. 按奇偶排序数组

  1. class Solution {
  2. public int[] sortArrayByParity(int[] A) {
  3. int[] result = new int[A.length];
  4. int first = 0;
  5. int last = A.length - 1;
  6. for (int j : A) {
  7. // 如果是偶数,放在数据最前面
  8. if (j % 2 == 0) {
  9. result[first] = j;
  10. first++;
  11. // 奇数放于数组最后面
  12. } else {
  13. result[last] = j;
  14. last--;
  15. }
  16. }
  17. return result;
  18. }
  19. }

908. 最小差值 I

  1. class Solution {
  2. public int smallestRangeI(int[] A, int K) {
  3. // 特殊情况排除
  4. if (A.length <= 1){
  5. return 0;
  6. }
  7. Arrays.sort(A);
  8. return Math.max((A[A.length - 1] - K) - (A[0] + K), 0);
  9. }
  10. }

914. 卡牌分组

  1. class Solution {
  2. public boolean hasGroupsSizeX(int[] deck) {
  3. int[] count = new int[10000];
  4. for (int c: deck) {
  5. count[c]++;
  6. }
  7. int g = -1;
  8. for (int i = 0; i < 10000; ++i) {
  9. if (count[i] > 0) {
  10. if (g == -1) {
  11. g = count[i];
  12. } else {
  13. g = gcd(g, count[i]);
  14. }
  15. }
  16. }
  17. return g >= 2;
  18. }
  19. public int gcd(int x, int y) {
  20. return x == 0 ? y : gcd(y % x, x);
  21. }
  22. }

917. 仅仅反转字母

  1. class Solution {
  2. public String reverseOnlyLetters(String S) {
  3. Stack<Character> letters = new Stack();
  4. for (char c: S.toCharArray())
  5. if (Character.isLetter(c))
  6. letters.push(c);
  7. StringBuilder ans = new StringBuilder();
  8. for (char c: S.toCharArray()) {
  9. if (Character.isLetter(c))
  10. ans.append(letters.pop());
  11. else
  12. ans.append(c);
  13. }
  14. return ans.toString();
  15. }
  16. }

922. 按奇偶排序数组 II

  1. class Solution {
  2. public int[] sortArrayByParityII(int[] nums) {
  3. int[] result = new int[nums.length];
  4. int j = 0;
  5. int k = 1;
  6. for (int num : nums) {
  7. if (isOrNot(num)) {
  8. result[j] = num;
  9. j += 2;
  10. } else {
  11. result[k] = num;
  12. k += 2;
  13. }
  14. }
  15. return result;
  16. }
  17. public static boolean isOrNot(int number){
  18. return number % 2 == 0;
  19. }
  20. }

925. 长按键入

  1. class Solution {
  2. public boolean isLongPressedName(String name, String typed) {
  3. int i = 0, j = 0;
  4. while (j < typed.length()) {
  5. if (i < name.length() && name.charAt(i) == typed.charAt(j)) {
  6. i++;
  7. j++;
  8. } else if (j > 0 && typed.charAt(j) == typed.charAt(j - 1)) {
  9. j++;
  10. } else {
  11. return false;
  12. }
  13. }
  14. return i == name.length();
  15. }
  16. }

929. 独特的电子邮件地址

  1. class Solution {
  2. public int numUniqueEmails(String[] emails) {
  3. Set<String> set = new HashSet<>();
  4. for (String email : emails) {
  5. StringBuilder stringBuilder = new StringBuilder();
  6. for (int i = 0; i < email.length(); i++) {
  7. if (email.charAt(i) == '.'){
  8. continue;
  9. }else if (email.charAt(i) == '+'){
  10. do {
  11. i++;
  12. }while (email.charAt(i) != '@');
  13. }
  14. if (email.charAt(i) == '@'){
  15. stringBuilder.append(email.substring(i));
  16. break;
  17. }
  18. stringBuilder.append(email.charAt(i));
  19. }
  20. set.add(stringBuilder.toString());
  21. }
  22. return set.size();
  23. }
  24. }

937. 重新排列日志文件

class Solution {
    public String[] reorderLogFiles(String[] logs) {
        Arrays.sort(logs, (log1, log2) -> {
            // 将数组中的元素分成两部分(标识符和内容)
            String[] split1 = log1.split("\\s+", 2);
            String[] split2 = log2.split("\\s+", 2);
            // 判断内容部分的第一个,如果是数字就是数字日志,如果是字母就是字母日志
            boolean isDigit1 = Character.isDigit(split1[1].charAt(0));
            boolean isDigit2 = Character.isDigit(split2[1].charAt(0));
            if (!isDigit1 && !isDigit2) {
                // 如果两个都不是数字日志判断字典顺序
                int cmp = split1[1].compareTo(split2[1]);
                // 如果字典顺序不相同,直接返回
                if (cmp != 0) return cmp;
                // 字典顺序相同就比较标识符
                return split1[0].compareTo(split2[0]);
            }
            // 返回(字母在前 —— 1,数字在后 —— -1)
            return isDigit1 ? (isDigit2 ? 0 : 1) : -1;
        });
        return logs;
        /*
        Arrays.sort(logs, new Comparator<String>() {
            @Override
            public int compare(String log1, String log2) {
                String[] split1 = log1.split("\\s+", 2);
                String[] split2 = log2.split("\\s+", 2);
                boolean isDigit1 = Character.isDigit(split1[1].charAt(0));
                boolean isDigit2 = Character.isDigit(split2[1].charAt(0));
                if (!isDigit1 && !isDigit2) {
                    int cmp = split1[1].compareTo(split2[1]);
                    if (cmp != 0) return cmp;
                    return split1[0].compareTo(split2[0]);
                }
                return isDigit1 ? (isDigit2 ? 0 : 1) : -1;
            }
        });
        */
        return logs;
    }
}

941. 有效的山脉数组

class Solution {
    public boolean validMountainArray(int[] arr) {
        int maxIndex = findMaxIndex(arr);
        if (arr.length < 3 || maxIndex == 0 || maxIndex == arr.length - 1){
            return false;
        }
        boolean flag = true;
        for (int i = 0; i < arr.length - 1; i++) {
            if (i < maxIndex){
                if (arr[i] >= arr[i + 1]) {
                    flag = false;
                }
            }else {
                if (arr[i] <= arr[i + 1]) {
                    flag = false;
                }
            }
        }
        return flag;
    }

    public static int findMaxIndex(int[] arr){
        int max = Integer.MIN_VALUE;
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max){
                max = arr[i];
                index = i;
            }
        }
        return index;
    }
}

942. 增减字符串匹配

class Solution {
    public int[] diStringMatch(String s) {
        int N = s.length();
        int[] arr = new int[N + 1];
        for (int i = 0; i < N + 1; i++) {
            arr[i] = i;
        }
        int[] result = new int[N + 1];
        int first = 0;
        int index = N;
        for (int i = 0; i < N; i++) {
            if (s.charAt(i) == 'I'){
                result[i] = arr[first++];
            }else {
                result[i] = arr[index--];
            }
        }
        result[N] = arr[index];
        return result;
    }
}

944. 删列造序

class Solution {
    public int minDeletionSize(String[] strs) {
        int length = strs[0].length();
        int result = 0;
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < strs.length - 1; j++) {
                if (strs[j].charAt(i) > strs[j + 1].charAt(i)){
                    result++;
                    break;
                }
            }
        }
        return result;
    }
}

953. 验证外星语词典

class Solution {
    public boolean isAlienSorted(String[] words, String order) {
        int[] index = new int[26];
        for (int i = 0; i < order.length(); ++i)
            index[order.charAt(i) - 'a'] = i;

        search: for (int i = 0; i < words.length - 1; ++i) {
            String word1 = words[i];
            String word2 = words[i+1];

            // Find the first difference word1[k] != word2[k].
            for (int k = 0; k < Math.min(word1.length(), word2.length()); ++k) {
                if (word1.charAt(k) != word2.charAt(k)) {
                    // If they compare badly, it's not sorted.
                    if (index[word1.charAt(k) - 'a'] > index[word2.charAt(k) - 'a'])
                        return false;
                    continue search;
                }
            }

            // If we didn't find a first difference, the
            // words are like ("app", "apple").
            if (word1.length() > word2.length())
                return false;
        }

        return true;
    }
}

961. 重复 N 次的元素

class Solution {
    public int repeatedNTimes(int[] A) {
        int len = A.length;
        int repeat = len / 2;
        for (int i : A) {
            if (count(A,i) == repeat) {
                return i;
            }
        }
        return 0;
    }

    public static int count(int[] A,int number){
        int result = 0;
        for (int i : A) {
            if (i == number){
                result++;
            }
        }
        return result;
    }
}

976. 三角形的最大周长

class Solution {
    public int largestPerimeter(int[] A) {
        Arrays.sort(A);
        for (int i = A.length - 1; i >= 2; --i) {
            if (A[i - 2] + A[i - 1] > A[i]) {
                return A[i - 2] + A[i - 1] + A[i];
            }
        }
        return 0;
    }
}

977. 有序数组的平方

class Solution {
    public int[] sortedSquares(int[] nums) {
        int[] result = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            result[i] = nums[i] * nums[i];
        }
        Arrays.sort(result);
        return result;
    }
}

985. 查询后的偶数和

class Solution {
    // 先求和,如果操作的位置为偶数则和做同样操作,否则直接跳过
    public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
        int S = 0;
        for (int x: A)
            if (x % 2 == 0)
                S += x;

        int[] ans = new int[queries.length];

        for (int i = 0; i < queries.length; ++i) {
            int val = queries[i][0], index = queries[i][1];
            if (A[index] % 2 == 0) S -= A[index];
            A[index] += val;
            if (A[index] % 2 == 0) S += A[index];
            ans[i] = S;
        }

        return ans;
    }
}

989. 数组形式的整数加法

class Solution {
    public List<Integer> addToArrayForm(int[] num, int k) {
        List<Integer> res = new ArrayList<Integer>();
        int n = num.length;
        for (int i = n - 1; i >= 0; --i) {
            int sum = num[i] + k % 10;
            k /= 10;
            if (sum >= 10) {
                k++;
                sum -= 10;
            }
            res.add(sum);
        }
        for (; k > 0; k /= 10) {
            res.add(k % 10);
        }
        Collections.reverse(res);
        return res;
    }
}

999. 可以被一步捕获的棋子数

class Solution {
    public int numRookCaptures(char[][] board) {
        int cnt = 0, st = 0, ed = 0;
        int[] dx = {0, 1, 0, -1};
        int[] dy = {1, 0, -1, 0};
        // 先找出R 车所在的位置
        for (int i = 0; i < 8; ++i) {
            for (int j = 0; j < 8; ++j) {
                if (board[i][j] == 'R') {
                    st = i;
                    ed = j;
                    break;
                }
            }
        }
        // 只有四个方位,分别往四个方位走
        for (int i = 0; i < 4; ++i) {
            for (int step = 0;; ++step) {
                int tx = st + step * dx[i];
                int ty = ed + step * dy[i];
                if (tx < 0 || tx >= 8 || ty < 0 || ty >= 8 || board[tx][ty] == 'B') {
                    break;
                }
                if (board[tx][ty] == 'p') {
                    cnt++;
                    break;
                }
            }
        }
        return cnt;
    }
}