题解 | #两数之和# continue & break

两数之和

https://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f

/**
  * 
  * @param numbers int整型一维数组 
  * @param target int整型 
  * @return int整型一维数组
  */
function twoSum( numbers ,  target ) {
    // write code here
    for(let i=0; i<numbers.length; i++) {
        let n1 = numbers[i]
        if (n1 > target) continue;
        let n2 = target - n1
        let j = numbers.lastIndexOf(n2)
        if (numbers.includes(n2) && j > 0 && j !== i) {
            return [i+1, j+1]
        }
    }
}
module.exports = {
    twoSum : twoSum
};

主要在于否定:

  1. 一旦发现大于target,直接continue
  2. 一旦找到答案,直接return,结束循环
全部评论

相关推荐

沉淀去了,8月是不是机会会多一点,。打招呼300+,就一个小厂面试,聊了十分钟天就让我去了,暑假继续沉淀了,到八月九月冲了
丰川打工祥:我目前的体感是,双非本+一段小厂实习,基本约不到中厂的面。已经开始第二段小厂了。可能的确是最近hc太少了。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务