LeetCode Brushing Diary Linked List II

1. Add four numbers II

topic description

insert image description here

problem solving ideas

1. Define a hash Map, where the key stores the sum of two numbers, and the value stores the two numbers and the number of occurrences.
2. Traverse and count the number of additions and occurrences of nums1 and nums elements (a+b).
3. Traverse nums3 and nums4, and sum (c+d), and count the number of times (0-(c+d)) appears in the Map.
4. Return the cumulative sum of occurrences of (0-(c+d)).

var fourSumCount = function(nums1, nums2, nums3, nums4) {
   let map = new Map()
   let result = 0;
   for(let i = 0;i<nums1.length;i++) {
       for(let j = 0;j<nums2.length;j++) { // 统计前两个数组元素之和
           let tmp = nums1[i] + nums2[j]
           if(map.has(tmp)){
               map.set(tmp,map.get(tmp) + 1)
           } else {
               map.set(tmp,1)
           }
       }
   }

   for(let i=0;i<nums3.length;i++) {
       for(let j = 0;j<nums4.length;j++) {
           let tmp = nums3[i] + nums4[j]
           if(map.has(0-tmp)){ // 如果map中存在 说明四数相加等于0
               result += map.get(0-tmp)
           }
       }
   }

   return result
};

2. Ransom letter

topic description

insert image description here

problem solving ideas

1. Define a hash Map, where the key stores each letter in ransomNote, and the value indicates the number of occurrences of each letter.
2. Traverse the ransomNote string and count the number of occurrences of each letter.
3. Traverse the magazine string, and subtract the corresponding number of times from each letter in the Map.
4. Traverse the map to determine whether there is a letter with a frequency greater than 0, and return false if there is, otherwise return true.

var canConstruct = function(ransomNote, magazine) {
    let map = new Map();
    for(let i = 0;i<ransomNote.length;i++) {
         map.set(ransomNote[i],(map.get(ransomNote[i])|| 0)+1)
    }

    for(let i = 0;i<magazine.length;i++) {
        if(map.has(magazine[i])) {
            map.set(magazine[i],map.get(magazine[i]) - 1)
        }
    }

    for(let [k,v] of map) {
        if(v > 0) {
            return false
        }
    }
    return true
};

3. The sum of three numbers

topic description

insert image description here

problem solving ideas

1. Sort the array in ascending order.
2. To traverse the array, use double pointers in the loop, where the left pointer represents the loop index + 1, and the right pointer is the last index of the array.
3. Determine the size of nums[i]+nums[left]+nums[right]=sum and 0.
4. If sum>0, the right pointer moves to the left, if sum<0, the left pointer moves to the right, if sum==0, add the corresponding data to the result.
5. Continue to do this in the next cycle, and pay attention to deduplication and pruning during the cycle.

var threeSum = function(nums) {
   let result = [];
   nums = nums.sort((a,b) => a-b)
   for(let i = 0;i<nums.length;i++) {
    if(nums[i] > 0) {
        return result
    }
    if(i>0&&nums[i] == nums[i-1]) {
        continue
    }
    let left = i + 1;
    let right = nums.length -1
    while(left<right) { // i j k 不相等 所以这儿不能又等于
       let tmp = nums[i] + nums[left] + nums[right]
       if(tmp > 0) {
           right --
       } else if(tmp<0) {
           left ++
       } else {
           result.push([nums[i],nums[left],nums[right]])

           while(left<right && nums[right] == nums[right - 1]) right--
           while(left<right && nums[left] == nums[left+1]) left++ // 去重


           left ++
           right -- // 区间缩小
       }
    }
   }

   return result
};

4. Sum of four numbers

topic description

insert image description here

problem solving ideas

The idea of ​​solving this question is the same as the sum of three numbers, just change the outer loop to a double loop.

var fourSum = function(nums, target) {
     nums = nums.sort((a,b) => a- b);
    const result = [];
    for(let i = 0;i<nums.length;i++) {
        if(nums[i]>target&&target>0){
            break; // 剪枝
        }
        if(i>0&&nums[i] === nums[i-1]){
            continue; // 去重
        }
        for(let j = i+1;j<nums.length-1;j++){
            if(nums[i] + nums[j] > target&&target>0){
                break; // 剪枝
            }
            if(j>i+1&&nums[j] == nums[j-1]){
                continue // 去重
            }
            const twoSum = nums[i] + nums[j]
            let left = j+1;
            let right = nums.length-1
            while(left<right){
                if(twoSum + nums[left]+nums[right] === target) {
                    result.push([nums[i],nums[j],nums[left],nums[right]])
                    while(right>left&&nums[right] == nums[right-1]) {
                        right--
                    }
                    while(right>left&&nums[left] == nums[left+1]){
                        left++
                    }
                    left++
                    right--;
                } else if(twoSum + nums[left] + nums[right] > target) {
                   right --
                  
                } else  {
                    left++;
                }
            } 
        }
    }

    return result
};

Guess you like

Origin blog.csdn.net/Salange1/article/details/128309298
Recommended