LeetCode-the number of times the number appears in the array (JS implementation)

Title description

Problem-solving ideas

Idea 1: Use a hash table (space complexity is not satisfied)

  • Express the key as an element of the array, and the value as the number of occurrences
// 方法1:使用Map数据结构
var singleNumbers = function(nums) {
    
    
    const m = new Map();
    for (let v of nums) {
    
    
        if (m.has(v)) {
    
    
            m.set(v,m.get(v) + 1)
        } else {
    
    
            m.set(v,1);
        }
    }
    result = [];
    for (let v of m) {
    
    
        if (v[1] === 1) {
    
    
            result.push(v[0])
        }
    }
    console.log(result);
    return result;
};

Idea 2: Use bit operations (multiple for loops: do not meet the requirements of space complexity)

  • Because the title says that except for the two numbers that appear once, the rest of the numbers appear twice. If they appear twice, the exclusive OR operation will be 0. Therefore, the value obtained by traversing the XOR must be those two only. Number that appears once.
  • According to the result of all traversal XOR, for example, it is 0111. From the last digit 1, we can see that the last digit of the two numbers that only appear once must be different, so we group according to this feature.
  • Those whose last digit is 1 are divided into 1 group, and those whose last digit is 1 are divided into 1 group.
  • The two groups are XORed separately, and the obtained value is then returned as the final result. It can be calculated on paper.
// 使用位运算的方法
var singleNumbers = function(nums) {
    
    
    let temp = 0;
    let temp2 = 0;
    let temp3 = 0;
    for (let v of nums) {
    
    
        temp = temp ^ v;
    }
    let temp1 = temp.toString(2);
    let flag = 1;
    for (let i = temp1.length-1;i >= 0;i--) {
    
    
        console.log(temp1[i]);
        if (temp1[i] === '0') {
    
    
            flag = flag + 1;
        } else {
    
    
            break;
        }
    }
    // 遍历每个数组,将数组中的值转为二进制
    const result = []
    for (let v of nums) {
    
    
        result.push(v.toString(2));
    }
    console.log(result);
    let arr1 = [];
    let arr2 = [];
    for (let v of result) {
    
    
        if (v[v.length-flag] === '1') {
    
    
            arr1.push(v);
        } else {
    
    
            arr2.push(v);
        }
    }
    console.log(flag);
    console.log(arr1);
    console.log(arr2);
    for (let i in arr1) {
    
    
        arr1[i] = parseInt(arr1[i],2)
    }
    for (let i in arr2) {
    
    
        arr2[i] = parseInt(arr2[i],2)
    }
    console.log(arr1);
    console.log(arr2);
    for (let v of arr1) {
    
    
        temp2 = temp2 ^ v;
    }
    for (let v of arr2) {
    
    
        temp3 = temp3 ^ v;
    }
    console.log(temp2,temp3);
    return [temp2,temp3];
};

The final solution (in accordance with the requirements of the subject)

  • Use bit operations
  1. First, let all the elements of the array be XORed in turn, and the value obtained from right to left is the first position of 1 is two different positions where the element only appears once.
  2. Use variables to record the position of the first 1 from right to left.
  3. The variable obtained in the second step is ANDed with each element in the array, and the above array can be divided into two groups
  4. These two groups are all XORed separately, and the two values ​​are obtained and then returned is the number that appears only once in the question.

Code

var singleNumbers = function(nums) {
    
    
    let temp = 0;
    let a = 0;
    let b = 0;
    for (let v of nums) {
    
    
        temp = temp ^ v;
    }
    console.log(temp);
    // 判断从右往左第几位是1
    let One_Location = 1;
    while ((temp & One_Location) === 0) {
    
    
        One_Location = One_Location << 1;
    }
    for (let v of nums) {
    
    
        if ((One_Location & v) === 0) {
    
    
            a = a ^ v;
        } else {
    
    
            b = b ^ v;
        }
    };
    return [a,b];
};

Guess you like

Origin blog.csdn.net/sinat_41696687/article/details/115261191