Menu Sidebar
Menu

Convert 1D Array Into 2D Array

给一个一维数组和整数m和n, 变成二维数组[m][n];

class Solution {
    public int[][] construct2DArray(int[] original, int m, int n) {
        int[][] res = new int[m][n];
        if(m*n != original.length)
            return new int[][]{};
        int p = 0;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                res[i][j] = original[p++];
            }
        }
        return res;
    }
}

Rings and Rods

给一个string. 里面有一组组的字符, 第一个是RGB代表颜色,第二个是这个颜色对应的位置[0,9]. 求[0,9]中有几个同时有RGB.

class Solution {
    public int countPoints(String rings) {
        int n = rings.length();
        int[][] ary = new int[10][3];
        for(int i = 0; i < n; i+=2){
            char c = rings.charAt(i);
            char nn = rings.charAt(i + 1);
            if(c == 'R'){
                ary[nn - '0'][0] ++;
            }else if(c == 'G')
                ary[nn - '0'][1]++;
            else
                ary[nn - '0'][2]++;
        }
        int res = 0;
        for(int i = 0; i <= 9; i++) {
            if(ary[i][0] > 0 && ary[i][1] > 0 && ary[i][2] > 0)
                res++;
        }
        return res;
    }
}

A Number After a Double Reversal

给个数字, 问翻转两次是不是和以前的数字一样.

class Solution {
    public boolean isSameAfterReversals(int num) {
        if(num == 0)
            return true;
        String s = String.valueOf(num);
        if(s.charAt(0) == '0')
            return false;
        if(s.charAt(s.length() - 1) == '0')
            return false;
        return true;
    }
}

Find First Palindromic String in the Array

给一个字符串组, 找到第一个回文

class Solution {
    public String firstPalindrome(String[] words) {
        for(String w : words){
            if(isPal(w))
                return w;
        }
        return "";
    }
    public boolean isPal(String s) {
        int i = 0;
        int j = s.length() - 1;
        while(i <= j){
            if(s.charAt(i) != s.charAt(j))
                return false;
            i++;
            j--;
        }
        return true;
    }
}

Find Subsequence of Length K With the Largest Sum

给一个数组, 求一个和最大的长度为k的子序列.

这有三个定语, 长度为k, 子序列, 和最大. 我用的是做一个pair<坐标, 数字>, 然后排序两次.

class Solution {
    public int[] maxSubsequence(int[] nums, int k) {
        List<Pair> pl= new ArrayList<>();
        for(int i = 0; i < nums.length; i++)
            pl.add(new Pair(i, nums[i]));
        Collections.sort(pl, (a,b) -> (b.s - a.s));
        List<Pair> spl= new ArrayList<>();
        for(int i = 0; i < k; i++){
            spl.add(pl.get(i));
        }
        Collections.sort(spl, (a,b) -> (a.f - b.f));
        int[] res = new int[k];
        for(int i = 0; i < k; i++){
            res[i] = spl.get(i).s;
        }
        return res;
    }
    class Pair{
        int f;
        int s;
        public Pair(int f, int s){
            this.f = f; this.s = s;
        }
    }
}

Find Target Indices After Sorting Array

给一个数组, 给一个整数target. 求这个整数在数组排序后中出现的位置.

class Solution {
    public List<Integer> targetIndices(int[] nums, int target) {
        Arrays.sort(nums);
        List<Integer> list = new ArrayList<>();
        for(int i = binary_search(nums, target); i < nums.length; i++){
            if(nums[i] == target)
                list.add(i);
            else
                break;
        }
        return list;
    }
    public int binary_search(int[] nums, int t) {
        int i = 0;
        int j = nums.length - 1;
        while(i < j){
            int m = i + (j - i) / 2;
            if(nums[m] >= t)
                j = m;
            else
                i = m + 1;
        }
        return i;
    } 
}

Count Common Words With One Occurrence

给两个字符串组, 找到其中出现一次的字符串的交集.

class Solution {
    public int countWords(String[] words1, String[] words2) {
        Map<String, Integer> count1 = new HashMap<>();
        Map<String, Integer> count2 = new HashMap<>();
        for(String w : words1)
            count1.put(w, count1.getOrDefault(w, 0) + 1);
        for(String w : words2)
            count2.put(w, count2.getOrDefault(w, 0) + 1);
        Set<String> set1 = new HashSet<>();
        Set<String> set2 = new HashSet<>();
        for(String w : words1)
            if(count1.get(w) == 1)
                set1.add(w);
        for(String w : words2)
            if(count2.get(w) == 1)
                set2.add(w);
        set1.retainAll(set2);
        return set1.size();
    }
}

Two Furthest Houses With Different Colors

给一个数组, 求两个不同数字的最大距离.

这题讨论里的人O(N^2)都100%, 我O(N*100)居然8%…

就是利用map的put的原理(重复覆盖相同元素). 然后再扫一次即可.

class Solution {
    public int maxDistance(int[] colors) {
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < colors.length; i++) {
            map.put(colors[i], i);
        }
        int res = 0;
        for(int i = 0; i < colors.length; i++) {
            for(int j = 0; j <= 100; j++) {
                if(j != colors[i]){
                    res = Math.max(res, map.getOrDefault(j, i) - i);
                }
            }
        }
        return res;
    }
}

Check if All A’s Appears Before All B’s

给一个string s, 只有a和b, 看看是不是所有的a都出现在b前边.

这题我看答案咋做的都有. 但是不是设置flag就是sort. flag容易有corn cases, sort明显不是o(n). 我用的是计数法. 先算下有多少a, 然后看看是不是都在b前边.

class Solution {
    public boolean checkString(String s) {
        int countA = 0;
        for(char c : s.toCharArray())
            if(c == 'a')
                countA++;
        for(char c : s.toCharArray()){
            if(c == 'a')
                countA--;
            else
                break;
        }
        if(countA == 0)
            return true;
        else
            return false;
    }
}
Newer Posts
Older Posts

书脊

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

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