README.md

April 3, 2018 · View on GitHub

Description

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

思路

二次循环即可。

kotlin

class Solution {
    fun twoSum(nums: IntArray, target: Int): IntArray {
        val arr: IntArray = intArrayOf(0, 0)
        for (i in 0 until nums.size - 1) {
            for (j in i + 1 until nums.size) {
                if (nums[i] + nums[j] == target) {
                    arr[0] = i
                    arr[1] = j
                    return arr
                }
            }
        }
        return arr
    }
}

Java

class Solution {
    public int[] twoSum(int[] nums, int target) {
        for(int i=0;i<nums.length;i++){
            for(int j=i+1;j<nums.length;j++){
                if(nums[i]+nums[j]==target){
                    return new int[]{i,j};
                }
            }
        }
        return null;
    }
}

JavaScript

var twoSum = function(nums, target) {
    for (let i = 0; i < nums.length - 1; i++) {
        res = target - nums[i]
        for (let j = i + 1; j < nums.length; j++) {
            if (nums[j] === res) {
                return [i, j]
            }
        }
        if (i === nums.length) {
            return []
        }
    }
}