Fork me on GitHub
打赏

LeetCode-1-Two Sum | 两数之和

题目

two-sum

解法

简单题。常规解法(解法1),用两个for循环来做,第一个循环从数组nums下标为0开始遍历,第二个循环从数组下标1开始遍历,如果没找到两数之和的target值,就将两个循环的下标加1,继续循环,直到找到目标值,返回结果并退出。

Golang

func twoSum(nums []int, target int) []int {
    for i, x := range nums {  //第一个循环
        for j := i + 1; j < len(nums); j++ {  //第二个循环
            if x+nums[j] == target {  //如果数组前一个值+后一个值 = target
                return []int{i, j}  //返回这两个值
            }
        }
    }
    return nil
}
posted @ 2021-01-01 20:58  Zoctopus_Zhang  阅读(76)  评论(0编辑  收藏  举报
// function btn_donateClick() { var DivPopup = document.getElementById('Div_popup'); var DivMasklayer = document.getElementById('div_masklayer'); DivMasklayer.style.display = 'block'; DivPopup.style.display = 'block'; var h = Div_popup.clientHeight; with (Div_popup.style) { marginTop = -h / 2 + 'px'; } } function MasklayerClick() { var masklayer = document.getElementById('div_masklayer'); var divImg = document.getElementById("Div_popup"); masklayer.style.display = "none"; divImg.style.display = "none"; } setTimeout( function () { document.getElementById('div_masklayer').onclick = MasklayerClick; document.getElementById('btn_donate').onclick = btn_donateClick; var a_gzw = document.getElementById("guanzhuwo"); a_gzw.href = "javascript:void(0);"; $("#guanzhuwo").attr("onclick","follow('33513f9f-ba13-e011-ac81-842b2b196315');"); }, 900);