Day1: two sum

1.暴力

  • Java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public class Solution {
    public int[] twoSum(int[] nums, int target) {
    int[] result = new int[2];
    for(int i = 0 ; i < nums.length; i++){
    for(int j = i + 1; j < nums.length; j++){
    if((nums[i]+nums[j])==target){
    result[0] = i;
    result[1] = j;
    return result;
    }
    }
    }
    return result;
    }
    }
    执行用时 : 25 ms
    内存消耗 : 44.8 MB
  • Python3
    ~Python3
    class Solution:

    def twoSum(self, nums: List[int], target: int) -> List[int]:
        for i in range(len(nums)):
            or j in range(i+1, len(nums)):
                if nums[i] + nums[j] == target:
                    return [i, j]