力扣---2020.9.4

carroll18發表於2020-12-02

257. 二叉樹的所有路徑

class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        dfs(root,"",res);
        return res;
    }

    public void dfs(TreeNode root,String str,List<String> res){
        if(root==null){
            return;
        }
        str += root.val;
        if(root.left ==null && root.right == null){
            res.add(str);
        }else{
            dfs(root.left,str+"->",res);
            dfs(root.right,str+"->",res);
        }
    }
}
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> paths = new ArrayList<String>();
        if (root == null) {
            return paths;
        }
        Queue<TreeNode> nodeQueue = new LinkedList<TreeNode>();
        Queue<String> pathQueue = new LinkedList<String>();

        nodeQueue.offer(root);
        pathQueue.offer(Integer.toString(root.val));

        while (!nodeQueue.isEmpty()) {
            TreeNode node = nodeQueue.poll(); 
            String path = pathQueue.poll();

            if (node.left == null && node.right == null) {
                paths.add(path);
            } else {
                if (node.left != null) {
                    nodeQueue.offer(node.left);
                    pathQueue.offer(new StringBuffer(path).append("->").append(node.left.val).toString());
                }

                if (node.right != null) {
                    nodeQueue.offer(node.right);
                    pathQueue.offer(new StringBuffer(path).append("->").append(node.right.val).toString());
                }
            }
        }
        return paths;
    }
}

78. 子集

class Solution {
    List<List<Integer>> list = new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
        List<Integer> track = new ArrayList<>();
        backtrack(nums,track,0);
        return list;
    }
    
    public void backtrack(int[] nums,List<Integer> track,int start){
        list.add(new ArrayList(track));
        for (int i = start; i < nums.length; i++) {
            track.add(nums[i]);
            backtrack(nums,track,i+1);
            track.remove(track.size()-1);
        }
    }
}
class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        res.add(new ArrayList<>());
        for(int i = 0;i < nums.length;i++){
            int all = res.size();
            for(int j = 0;j < all;j++){
                List<Integer> tmp = new ArrayList<>(res.get(j));
                tmp.add(nums[i]);
                res.add(tmp);
            }
        }
        return res;
    }
}

461. 漢明距離

class Solution {
    public int hammingDistance(int x, int y) {
        return Integer.bitCount(x ^ y); 
    }
}
class Solution {
    public int hammingDistance(int x, int y) {
        int res = 0;
        int n = x ^ y;
        while(n>0){
            res += n&1;
            n >>= 1;
        }
        return res;
    }
}
class Solution {
    public int hammingDistance(int x, int y) {
        int count=0;
        if(x==y)
            return 0;
        while(x!=0||y!=0){
            if(x%2!=y%2)
                count++;
            x=x/2;
            y=y/2;
        }
        return count;
    }
}
class Solution {
    public int hammingDistance(int x, int y) {
        int count=0;
        int n = x^y;
        while(n!=0){
            count++;
            n = n&(n-1);
        }
        return count;
    }
}

你知道的越多,你不知道的越多。

相關文章