力扣---2020.3.27

914. 卡牌分组

class Solution {
    public boolean hasGroupsSizeX(int[] deck) {
        // 计数
        int[] counter = new int[10000];
        for (int num: deck) {
            counter[num]++;
        }
        // 求最大公约数
        return Arrays.stream(counter).reduce(this::gcd).getAsInt() > 1;
    }

    private int gcd(int a, int b) {
        return b == 0? a: gcd(b, a % b);
    }
}
class Solution {
    public boolean hasGroupsSizeX(int[] deck) {
        // 计数
        int[] counter = new int[10000];
        for (int num: deck) {
            counter[num]++;
        }
        // 求gcd
        int x = 0;
        for(int cnt: counter) {
            if (cnt > 0) {
                x = gcd(x, cnt); 
                if (x == 1) { 
                    return false;
                }
            }
        }
        return x >= 2;
    }
    
    // 辗转相除法
    private int gcd (int a, int b) {
        return b == 0? a: gcd(b, a % b);
    }
}

面试题32 - I. 从上到下打印二叉树

class Solution {
    public int[] levelOrder(TreeNode root) {
        if(root == null) return new int[0];

        Queue<TreeNode> q = new LinkedList<>();
        List<Integer> list = new ArrayList<>();
        q.offer(root);
        
        while(!q.isEmpty()){
            TreeNode node = q.poll();
            list.add(node.val);
            if(node.left != null) q.offer(node.left);
            if(node.right != null) q.offer(node.right);
        }
        
        int[] res = new int[list.size()];
        for(int i = 0;i < list.size();i++){
            res[i] = list.get(i);
        }
        return res;

    }
}

面试题50. 第一个只出现一次的字符

class Solution {
    public char firstUniqChar(String s) {
        if(s == null||s.length()==0) return ' ';
        LinkedHashMap<Character,Integer> map = new LinkedHashMap<>();
        for(char c:s.toCharArray()){
            map.put(c,map.getOrDefault(c,0)+1);
        }
        for(char c:map.keySet()){
            if(map.get(c)==1){
                return c;
            }
        }
        return ' ';
    }
}
class Solution {
    public char firstUniqChar(String s) {
        // hashmap
        HashMap<Character,Integer> map = new HashMap();
        char[] schar = s.toCharArray();
        for(char c:schar){
            if(!map.containsKey(c)){
                map.put(c,1);
            }else{
                map.put(c,2);
            }
        }

        for(char c:schar){
            if(map.get(c)==1){
                return c;
            }
        }
        return ' ';
    }
}
class Solution {
    public char firstUniqChar(String s) {
        int[] map = new int[256];
        char[] str = s.toCharArray();
        int len = s.length();
        for(int i = 0 ; i < len ; i++){
            map[(int)str[i]] += 1;
        }
        for(int i = 0 ; i < len ; i++){
            if(map[(int)str[i]] == 1) return str[i];
        }
        return ' ';
    }
}

你知道的越多,你不知道的越多。
有道无术,术尚可求,有术无道,止于术。
如有其它问题,欢迎大家留言,我们一起讨论,一起学习,一起进步

发布了213 篇原创文章 · 获赞 263 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_40722827/article/details/105150065