12.13 Make-up week

Record 2

Leetcode 104
recursion The
non-recursive stack will not write
Binary tree an
ideas yet.
Use DFS, deep recursion; the total depth is the maximum depth of the left stone subtree plus 1; the
depth of the empty tree is 0.

Code

// An highlighted block
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    
    
    public int maxDepth(TreeNode root) {
    
    
        if(root == null) {
    
    
            return 0;
        } else {
    
    
            int left = maxDepth(root.left);
            int right = maxDepth(root.right);
            return Math.max(left, right) + 1;
        }
    }
}

The second question
136 I
thought it was a simple question. It would be fine to traverse the records that only appear once,
but the idea of ​​using XOR operation to reduce the time complexity is too amazing for me...

The hash table is still learning...
A number that appears only once

Ideas

  1. Use collections to store numbers. Traverse each number in the array, if there is no such number in the set, add the number to the set, if there is already the number in the set, delete the number from the set, and the last remaining number is the number that appears only once .
  2. Use a hash table to store each number and the number of times that number appears. Traverse the array to get the number of occurrences of each number, update the hash table, and finally traverse the hash table to get the number that appears only once.
    Hash table is the best thing to think about...
  3. Use a collection to store all the numbers that appear in the array, and calculate the sum of the elements in the array. Since the set guarantees that there is no repetition of elements, the calculation is twice the sum of all the elements in the set, that is, the sum of the elements when each element appears twice. Since only one element in the array appears once, and the remaining elements appear twice, the sum of the elements in the array is subtracted from twice the sum of the elements in the set. The remaining number is the number that appears only once in the array.
    This uses numerical calculations.
    But it all requires the complexity of o(N).
    To achieve linear order,
    use the same XOR operation with the commutative law of XOR to be 0. Finally, the result of 0 and the single number operation is himself, this The time complexity is O(1).
    Code

A number of hash set ideas corresponds to a Count

class Solution {
    
    
    public int singleNumber(int[] nums) {
    
    
        Map<Integer, Integer> map = new HashMap<>();
        for (Integer i : nums) {
    
    
            Integer count = map.get(i);
            count = count == null ? 1 : ++count;
            map.put(i, count);
        }
        for (Integer i : map.keySet()) {
    
    
            Integer count = map.get(i);
            if (count == 1) {
    
    
                return i;
            }
        }
        return -1; // can't find it.
    }
}

Collective ideas

public static int singleNumber_HashSet(int[] nums) {
    
    

    int len = nums.length;
    Set<Integer> set = new HashSet<>();

    for (int i = 0; i < len; i++) {
    
    
        // 尝试将当前元素加入 set
        if (!set.add(nums[i])) {
    
    
            // 当前元已经存在于 set,即当前元素第二次出现,从 set 删除
            set.remove(nums[i]);
        }
    }

    // 最后只剩一个不重复的元素
    return set.iterator().next();
}

XOR
I can't think of it

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

Guess you like

Origin blog.csdn.net/weixin_46338641/article/details/111341298