每日刷题day12---两数之和

110 阅读1分钟

leetcode传送门

image.png

双指针:

1) 确定双指针位置(都在起始位置,一个在起始位置,一个在末尾位置)
(2)  结束条件 (双指针重合,high到末尾)
(3while条件判断(双指针走向 => 同向走,向中间靠拢)

思路:双指针与滑动窗口 => 窗口的左右两边就是两个指针,根据窗口内值之和确定窗口的位置和宽

终止条件:双指针重合

/**
* @param {number} target
* @return {number[][]}
*/
var findContinuousSequence = function(target) {
   let low = 1
   let high  = 2
   let res = []
   while(low<high){
       let s = (low + high)*(high - low +1) /2
       if(s === target) {
           let set  = new Set()
           for(let i =low;i<=high;i++){
               set.add(i)
           }
           res.push(Array.from(set))
           low ++ 
       }
       else if(s<target){
           high++
       }else{
           low++
       }
   }
   return res
};