Menu Sidebar
Menu

Remove All Ones With Row and Column Flips

翻一个只有0和1的矩阵, 只能翻一个行或者一个列. 返回能不能翻成全1或者全0矩阵.

这题只需要判断第一行, 见到0不动, 见到1翻过来,这样第一行就是全0. 然后通过判断其他行是不是全1或者全0, 即可知道答案.

证: 如果第一行已经全0, 其他某行, 非全1或者全0, 那么这行通过行翻转, 肯定不能得到全1或者全0, 但是通过列翻转, 又破坏第一行的全0,故此.

class Solution {
    public boolean removeOnes(int[][] grid) {
        int n = grid.length;
        int m = grid[0].length;
        
        for(int i = 0; i < n; i++) {
            if(i == 0){ // if first row
                for(int j = 0; j < m; j++) {
                    if(grid[i][j] == 0){
                        for(int k = 0; k < n; k++){
                            flip(grid, k, j);
                        }
                    }
                }
            }
            else
            {
                for(int j = 1; j < m; j++) {
                    if(grid[i][j] != grid[i][j - 1])
                        return false;
                }
            }
        }
        return true;
    }
    
    public void flip(int[][] grid, int i, int j){
        if(grid[i][j] == 0)
            grid[i][j] = 1;
        else
            grid[i][j] = 0;
    }
   
}

Capitalize the Title

给一个string, 是一段英文, 除了长度为1或者2的单词全部小写外, 大写每个单词的首字母.

class Solution {
    public String capitalizeTitle(String title) {
        String[] strs = title.split(" ");
        StringBuffer sb = new StringBuffer();
        for(String s : strs)
        {
            String ls = s.toLowerCase();
            if(ls.length() == 1 || ls.length() == 2)
                sb.append(ls);
            else
                sb.append(Character.toUpperCase(ls.charAt(0)) + ls.substring(1));
            sb.append(" ");
        }
        return sb.toString().trim();
    }
}

Check if Every Row and Column Contains All Numbers

给一个矩阵, 判断row和col是不是都是有[1,n]n个不同的数字组成.

class Solution {
    public boolean checkValid(int[][] matrix) {
        int n = matrix.length;
        for(int i = 0; i < n; i++) { // row
            Set<Integer> set = new HashSet<>();
            for(int j = 0; j < n; j++) {
                if(1 <= matrix[i][j] && matrix[i][j] <= n)
                    set.add(matrix[i][j]);
            }
            if(set.size() != n)
                return false;
        }
        for(int i = 0; i < n; i++) { // col
            Set<Integer> set = new HashSet<>();
            for(int j = 0; j < n; j++) {
                if(1 <= matrix[j][i] && matrix[j][i] <= n)
                    set.add(matrix[j][i]);
            }
            if(set.size() != n)
                return false;
        }
        return true;
    }
}

Check Whether Two Strings are Almost Equivalent

给两个字符串, 问是不是对于每个26个字母, 两个字符串的个数差不多于3个.

class Solution {
    public boolean checkAlmostEquivalent(String word1, String word2) {
        Map<Character, Integer> m1 = new HashMap<>();
        Map<Character, Integer> m2 = new HashMap<>();
        for(char c : word1.toCharArray())
            m1.put(c, m1.getOrDefault(c, 0) + 1);
        for(char c : word2.toCharArray())
            m2.put(c, m2.getOrDefault(c, 0) + 1);
        int res = 0;
        for(int i = 0; i < 26; i++) {
            char c = (char)('a' + i);
            int f1 = m1.getOrDefault(c, 0);
            int f2 = m2.getOrDefault(c, 0);
            if(Math.abs(f1 - f2) > 3)
                return false;
        }
        return true;
    }
}

Count Vowel Substrings of a String

找出所有包含五个元音字符的substring.

这题就是滑窗.

class Solution {
    public int countVowelSubstrings(String word) {
        int res = 0;
        for(int i = 0; i < word.length(); i++) {
            if(!check(word.charAt(i)))
                continue;
            else
            {
                Set<Character> set = new HashSet<>();
                for(int j = i; j < word.length(); j++) {
                    if(!check(word.charAt(j)))
                        break;
                    set.add(word.charAt(j));
                    if(set.size() == 5) // contain all fvie vowels
                        res++;
                }
            }
        }
        return res;
    }
    
    public boolean check(char c) {
        return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
    }
}

Kth Distinct String in an Array

给一个字符串组, 求第k个只出现一次的字符串.

class Solution {
    public String kthDistinct(String[] arr, int k) {
        Map<String, Integer> map = new HashMap<>();
        for(String s : arr)
            map.put(s, map.getOrDefault(s, 0) + 1);
        int j = 0;
        for(String s : arr){
            if(map.get(s) == 1)
                j++;
            if(j == k)
                return s;
        }
        return "";
    }
}

Number of Valid Words in a Sentence

这题就是规则检查, 做的时候细心一点即可.

class Solution {
    public int countValidWords(String sentence) {
        String[] strs = sentence.split(" ");
        int res = 0;
        for(String s : strs)
            if(!s.isEmpty()&& check(s.trim()))
                res++;
        return res;
    }
    
    public boolean check(String s) {
        int h = 0;
        for(int i = 0; i < s.length(); i++) {
            if(Character.isLowerCase(s.charAt(i))){
                continue;
            } else if(s.charAt(i) == '-' && i > 0 && Character.isLowerCase(s.charAt(i - 1)) && i+1 < s.length() && Character.isLowerCase(s.charAt(i + 1))){
                h++;
                continue;
            } else if((s.charAt(i) == '.' || s.charAt(i) == '!' || s.charAt(i) == ',') && i + 1 == s.length())
                continue;
            else
                return false;
        }
        return h <= 1;
    }
}

Minimum Number of Moves to Seat Everyone

给两个数组, 第一个代表椅子的位置, 第二个代表学生的位置, 求如何让学生走向最近的椅子.

因为最近的椅子肯定不会是和其他同学与椅子交叉的的椅子, 所以两边都排序即可.

class Solution {
    public int minMovesToSeat(int[] seats, int[] students) {
        Arrays.sort(seats);
        Arrays.sort(students);
        int res = 0;
        for(int i = 0; i< seats.length; i++){
            res += Math.abs(seats[i] - students[i]);
        }
        return res;
    }
}

Two Out of Three

给三个数组, 求哪些数字出现在这三个数组中至少出现两次.

class Solution {
    public List<Integer> twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {
        Map<Integer, Integer> map = new HashMap<>();
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        Set<Integer> set3 = new HashSet<>();
        for(int n : nums1)
            set1.add(n);
        for(int n : nums2)
            set2.add(n);
        for(int n : nums3)
            set3.add(n);
        for(int n : set1)
            map.put(n, map.getOrDefault(n, 0) + 1);
       for(int n : set2)
            map.put(n, map.getOrDefault(n, 0) + 1);
       for(int n : set3)
            map.put(n, map.getOrDefault(n, 0) + 1);
        List<Integer> res = new LinkedList<>();
        for(Map.Entry<Integer, Integer> e : map.entrySet())
        {
            if(e.getValue() >= 2)
                res.add(e.getKey());
        }
        return res;
    }
}

Minimum Moves to Convert String

给一个字符串s,给一个操作把连续三个字符串变成O, 求几次能能全变成O.

这题就是找到X, 然后做一次操作, 然后跳过3个字符, 继续.

class Solution {
    public int minimumMoves(String s) {
        int res = 0;
        for(int i = 0; i < s.length();){
            if(s.charAt(i) == 'X'){
                res++;
                i += 3;
            }
            else
            {
                i++;
            }
        }
        return res;
    }
}
Newer Posts
Older Posts

书脊

这青苔碧瓦堆, 俺曾睡风流觉, 将五十年兴亡看饱.

February 2025
M T W T F S S
 12
3456789
10111213141516
17181920212223
2425262728