804. 唯一摩尔斯密码词

  1. class Solution {
  2. public int uniqueMorseRepresentations(String[] words) {
  3. String[] morse = new String[]{
  4. ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",
  5. ".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",
  6. ".--","-..-","-.--","--.."
  7. };
  8. Set<String> set = new HashSet<>();
  9. Map<Character,Integer> map = new HashMap<>();
  10. for (int i = 0; i < 26; i++) {
  11. map.put((char)(i +97),i);
  12. }
  13. for (String word : words) {
  14. StringBuilder getMorse = new StringBuilder();
  15. for (int i = 0; i < word.length(); i++) {
  16. getMorse.append(morse[map.get(word.charAt(i))]);
  17. }
  18. set.add(getMorse.toString());
  19. }
  20. return set.size();
  21. }
  22. }

806. 写字符串需要的行数

  1. class Solution {
  2. public int[] numberOfLines(int[] widths, String s) {
  3. Map<Character,Integer> map = new HashMap<>();
  4. for (int i = 0; i < widths.length; i++) {
  5. map.put((char)(i + 97) ,widths[i]);
  6. }
  7. int[] result = new int[2];
  8. int line = 1;
  9. int count = 0;
  10. for (int i = 0; i < s.length(); i++) {
  11. count += map.get(s.charAt(i));
  12. if (count > 100){
  13. count = 0;
  14. count += map.get(s.charAt(i));
  15. line++;
  16. }
  17. }
  18. result[0] = line;
  19. result[1] = count;
  20. return result;
  21. }
  22. }

811. 子域名访问计数

  1. class Solution {
  2. public List<String> subdomainVisits(String[] cpdomains) {
  3. Map<String, Integer> counts = new HashMap();
  4. for (String domain: cpdomains) {
  5. // 按空格进行split
  6. String[] cpinfo = domain.split("\\s+");
  7. // 按空格split之后的后半部分按.split
  8. String[] frags = cpinfo[1].split("\\.");
  9. int count = Integer.parseInt(cpinfo[0]);
  10. String cur = "";
  11. for (int i = frags.length - 1; i >= 0; --i) {
  12. cur = frags[i] + (i < frags.length - 1 ? "." : "") + cur;
  13. counts.put(cur, counts.getOrDefault(cur, 0) + count);
  14. }
  15. }
  16. List<String> ans = new ArrayList<>();
  17. for (String dom: counts.keySet())
  18. ans.add("" + counts.get(dom) + " " + dom);
  19. return ans;
  20. }
  21. }

812. 最大三角形面积

  1. class Solution {
  2. public double largestTriangleArea(int[][] points) {
  3. double S = 0;
  4. double x1 , x2 , x3, y1 , y2 , y3;
  5. for(int i = 0;i < points.length;i++){
  6. x1 = points[i][0];
  7. y1 = points[i][1];
  8. for(int j = i+1;j < points.length;j++){
  9. x2 = points[j][0];
  10. y2 = points[j][1];
  11. for(int k = j+1;k < points.length;k++){
  12. x3 = points[k][0];
  13. y3 = points[k][1];
  14. //坐标公式
  15. S = Math.max(S , Math.abs(x1*y2+x2*y3+x3*y1-x2*y1-x3*y2-x1*y3)/2.0);
  16. }
  17. }
  18. }
  19. return S;
  20. }
  21. }

819. 最常见的单词

  1. class Solution {
  2. public String mostCommonWord(String paragraph, String[] banned) {
  3. paragraph = paragraph.toLowerCase();
  4. for (String s : banned) {
  5. paragraph = paragraph.replaceAll(s,"1");
  6. }
  7. // 匹配所有标点符号
  8. paragraph = paragraph.replaceAll("[^0-9A-z\\u4e00-\\u9fa5\\s]", " ");
  9. String[] split = paragraph.split("\\s+");
  10. int max = 0;
  11. String result = "";
  12. for (int i = 0; i < split.length; i++) {
  13. if (split[i].contains("1")){
  14. continue;
  15. }
  16. int count = 1;
  17. for (int j = i + 1; j < split.length; j++) {
  18. if (split[i].contains("1")){
  19. continue;
  20. }
  21. if (split[j].equals(split[i])){
  22. count ++;
  23. }
  24. }
  25. if (max < count){
  26. max = count;
  27. result = split[i];
  28. }
  29. }
  30. return result;
  31. }
  32. }

821. 字符的最短距离

  1. class Solution {
  2. public int[] shortestToChar(String s, char c) {
  3. int[] result = new int[s.length()];
  4. Arrays.fill(result,Integer.MAX_VALUE);
  5. List<Integer> index = findIndex(s, c);
  6. for (int i = 0; i < s.length(); i++) {
  7. for (Integer integer : index) {
  8. result[i] = Math.abs(Math.min(integer - i,result[i]));
  9. }
  10. }
  11. return result;
  12. }
  13. public List<Integer> findIndex(String s,char c){
  14. List<Integer> list = new ArrayList<>();
  15. for (int i = 0; i < s.length(); i++) {
  16. if (s.charAt(i) == c){
  17. list.add(i);
  18. }
  19. }
  20. return list;
  21. }
  22. }

824. 山羊拉丁文

  1. class Solution {
  2. public String toGoatLatin(String S) {
  3. // 特殊条件排除
  4. if (S.length() == 0){
  5. return "";
  6. }
  7. // 元音字母字符串,用作匹配
  8. String vowel = "aeiouAEIOU";
  9. // 按空格拆分成数组
  10. String[] words = S.split("\\s+");
  11. StringBuilder result = new StringBuilder();
  12. for (int i = 0; i < words.length; i++) {
  13. // 元音字母开头直接添加原字符再拼接ma
  14. if (vowel.contains(words[i].substring(0,1))){
  15. result.append(words[i]);
  16. }else {
  17. // 辅音字母开头换头至尾再添加ma
  18. result.append(words[i].substring(1));
  19. result.append(words[i].charAt(0));
  20. }
  21. result.append("ma");
  22. int k = i;
  23. // 根据字母顺序添加a
  24. do {
  25. result.append("a");
  26. k --;
  27. }while (k >= 0);
  28. // 每拼接完一个单词添加一个空格
  29. if (i != words.length - 1){
  30. result.append(" ");
  31. }
  32. }
  33. return result.toString();
  34. }
  35. }

830. 较大分组的位置

class Solution {
    public int[][] flipAndInvertImage(int[][] image) {
        for (int i = 0; i < image.length; i++) {
            for (int j = 0; j <= image[0].length / 2; j++) {
                int k = image[0].length - 1;
                while (k >= image[0].length / 2) {
                    int temp = image[i][j];
                    image[i][j] = image[i][k];
                    image[i][k] = temp;
                    k--;
                    j++;
                }
            }
        }
        for (int i = 0; i < image.length; i++) {
            for (int j = 0; j < image[0].length; j++) {
                if (image[i][j] == 1){
                    image[i][j] = 0;
                }else {
                    image[i][j] = 1;
                }
            }
        }
        return image;
    }
}

836. 矩形重叠

class Solution {
    public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
        if (rec1[0] == rec1[2] || rec1[1] == rec1[3] || rec2[0] == rec2[2] || rec2[1] == rec2[3]) {
            return false;
        }
        return (rec1[2] > rec2[0] &&   // left
                rec1[3] > rec2[1] &&   // bottom
                rec1[0] < rec2[2] &&   // right
                rec1[1] < rec2[3]);    // top
    }
}

844. 比较含退格的字符串

class Solution {
    public boolean backspaceCompare(String S, String T) {
        StringBuilder s = new StringBuilder();
        StringBuilder t = new StringBuilder();

        for (int i = 0; i < S.length(); i++) {
            if (S.charAt(i) != '#'){
                s.append(S.charAt(i));
            }else {
                if (s.length() != 0) {
                    s.delete(s.length() - 1,s.length());
                }
            }
        }

        for (int i = 0; i < T.length(); i++) {
            if (T.charAt(i) != '#'){
                t.append(T.charAt(i));
            }else {
                if (t.length() != 0) {
                    t.delete(t.length() - 1,t.length());
                }
            }
        }

        return s.compareTo(t) == 0;
    }
}

852. 山脉数组的峰顶索引

class Solution {
    public int peakIndexInMountainArray(int[] arr) {
        // 初步理解为找山峰位置(最大值)

        int max = 0;
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > arr[max]){
                max = i;
            }
        }
        return max;
    }
}

859. 亲密字符串

class Solution {
    public boolean buddyStrings(String a, String b) {
        if (a.length() == 0 || b.length() == 0){
            return false;
        }
        char[] chars = a.toCharArray();
        char[] chars1 = b.toCharArray();
        List<Integer> difference = new ArrayList<>();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] != chars1[i]){
                difference.add(i);
            }
        }
        // 如果没有不同 则判断A中是否有两个或两个以上相同元素
        if (difference.size() == 0){
            for (int i = 0; i < chars.length; i++) {
                int count = 1;
                for (int j = i + 1; j < chars.length; j++) {
                    if (chars[i] == chars[j]){
                        count++;
                    }
                }
                if (count >= 2){
                    return true;
                }
            }
            return false;
        }

        // 如果有两个以上的不同,则对数组进行交换,判断交换后的数组是否相同
        if (difference.size() == 2){
            char temp = chars[difference.get(0)];
            chars[difference.get(0)] = chars[difference.get(1)];
            chars[difference.get(1)] = temp;
        }

        return Arrays.equals(chars, chars1);
    }
}

860. 柠檬水找零

class Solution {
    public boolean lemonadeChange(int[] bills) {
        int five = 0, ten = 0;
        for (int bill : bills) {
            if (bill == 5) {
                five ++;
            } else if (bill == 10) {
                if (five == 0) {
                    return false;
                }
                five --;
                ten ++;
            } else {
                if (five > 0 && ten > 0) {
                    five --;
                    ten --;
                } else if (five >= 3) {
                    five -= 3;
                } else {
                    return false;
                }
            }
        }
        return true;
    }
}

867. 转置矩阵

class Solution {
    public int[][] transpose(int[][] matrix) {
        int[][] transpose = new int[matrix[0].length][matrix.length];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                transpose[j][i] = matrix[i][j];
            }
        }
        return transpose;
    }
}

868. 二进制间距

class Solution {
    public int binaryGap(int n) {
        // 将n转换为二进制字符串
        String binaryString = Integer.toBinaryString(n);
        int result = 0;
        List<Integer> list = new ArrayList<>();
        // 将所有的1下标的数放入list中
        for (int i = 0; i < binaryString.length(); i++) {
            if (binaryString.charAt(i) == '1'){
                list.add(i);
            }
        }
        // 如果list中只有0个或1个数,则说明没有相邻的1,返回0
        if (list.size() == 0 || list.size() == 1){
            return 0;
        }

        // 如果有相邻的1,距离最近的两个1一定是互相邻的1,找最大值
        for (int i = 0; i < list.size() - 1; i++) {
            result = Math.max(result,list.get(i + 1) - list.get(i));
        }

        return result;
    }
}

874. 模拟行走机器人

class Solution {
    public int robotSim(int[] commands, int[][] obstacles) {
        int[] dx = new int[]{0, 1, 0, -1};
        int[] dy = new int[]{1, 0, -1, 0};
        int x = 0, y = 0, di = 0;

        // Encode obstacles (x, y) as (x+30000) * (2^16) + (y+30000)
        Set<Long> obstacleSet = new HashSet();
        for (int[] obstacle: obstacles) {
            long ox = (long) obstacle[0] + 30000;
            long oy = (long) obstacle[1] + 30000;
            obstacleSet.add((ox << 16) + oy);
        }

        int ans = 0;
        for (int cmd: commands) {
            if (cmd == -2)  //left
                di = (di + 3) % 4;
            else if (cmd == -1)  //right
                di = (di + 1) % 4;
            else {
                for (int k = 0; k < cmd; ++k) {
                    int nx = x + dx[di];
                    int ny = y + dy[di];
                    long code = (((long) nx + 30000) << 16) + ((long) ny + 30000);
                    if (!obstacleSet.contains(code)) {
                        x = nx;
                        y = ny;
                        ans = Math.max(ans, x*x + y*y);
                    }
                }
            }
        }

        return ans;
    }
}

883. 三维形体投影面积

class Solution {
    public int projectionArea(int[][] grid) {
        int N = grid.length;
        int ans = 0;

        for (int i = 0; i < N;  ++i) {
            int bestRow = 0;  // largest of grid[i][j]
            int bestCol = 0;  // largest of grid[j][i]
            for (int j = 0; j < N; ++j) {
                if (grid[i][j] > 0) ans++;  // top shadow
                bestRow = Math.max(bestRow, grid[i][j]);
                bestCol = Math.max(bestCol, grid[j][i]);
            }
            ans += bestRow + bestCol;
        }

        return ans;
    }
}

884. 两句话中的不常见单词

class Solution {
    public String[] uncommonFromSentences(String A, String B) {
        String[] splitA = A.split("\\s+");
        String[] splitB = B.split("\\s+");

        List<String> arr = new ArrayList<>(Arrays.asList(splitA));
        arr.addAll(Arrays.asList(splitB));

        List<String> result = new ArrayList<>();

        for (int i = 0; i < arr.size(); i++) {
            int count = 1;
            for (int j = 0; j < arr.size(); j++) {
                if (j == i) continue;
                if (arr.get(i).equals(arr.get(j))) {
                    count++;
                    break;
                }
            }
            if (count == 1){
                result.add(arr.get(i));
            }
        }

        return result.toArray(new String[0]);
    }
}

888. 公平的糖果棒交换

class Solution {
    public int[] fairCandySwap(int[] A, int[] B) {
        // A中的元素总和减去一个自己的元素加上一个B中的元素如果等于B中的元素总和减去一个自己的元素加上一个A中的元素则说这是一个可用的方案

        // 定义一个接收返回的数组result
        int[] result = new int[2];

        int sumA = 0, sumB = 0;
        for (int i : A) {
            sumA += i;
        }

        for (int i : B) {
            sumB += i;
        }

        for (int j : A) {
            for (int k : B) {
                if (sumA - j + k == sumB - k + j) {
                    result[0] = j;
                    result[1] = k;
                    return result;
                }
            }
        }
        return result;
    }
}

892. 三维形体的表面积

class Solution {
    public int surfaceArea(int[][] grid) {
        if (grid == null || grid.length < 1 || grid[0].length < 1) return 0;
        //统计所有的立方体数量
        int blocks = 0;
        //统计有多少个面被其他面盖住,那么就在所有的立方体的表面积上减去被盖住的面数×2(因为盖住一个面需要另一个面来盖,所以会损失2个面);
        int cover = 0;
        for (int i = 0; i < grid.length; ++i) {
            for (int j = 0; j < grid[0].length; ++j) {
                blocks += grid[i][j];
                //这个是统计当前格子中因为堆叠而盖住了几个面
                cover += grid[i][j] > 1 ? grid[i][j] - 1 : 0;
                if (i > 0) {
                    //看看上一行同一列盖住了多少个面
                    cover += Math.min(grid[i - 1][j], grid[i][j]);
                }
                if (j > 0) {
                    //看看同一行前一列盖住了几个面
                    cover += Math.min(grid[i][j - 1], grid[i][j]);
                }
            }
        }
        return blocks * 6 - cover * 2;
    }
}

893. 特殊等价字符串组

class Solution {
    public int numSpecialEquivGroups(String[] A) {
        Set<String> seen = new HashSet<>();
        for (String S: A) {
            int[] count = new int[52];
            for (int i = 0; i < S.length(); ++i)
                count[S.charAt(i) - 'a' + 26 * (i % 2)]++;
            seen.add(Arrays.toString(count));
        }
        return seen.size();
    }
}

905. 按奇偶排序数组

class Solution {
    public boolean isMonotonic(int[] A) {
        return isSorted(A, true) || isSorted(A, false);
    }

    public boolean isSorted(int[] A, boolean increasing) {
        int n = A.length;
        if (increasing) {
            for (int i = 0; i < n - 1; ++i) {
                if (A[i] > A[i + 1]) {
                    return false;
                }
            }
        } else {
            for (int i = 0; i < n - 1; ++i) {
                if (A[i] < A[i + 1]) {
                    return false;
                }
            }
        }
        return true;
    }
}