两数之和 (leetcode1)

一:解题思路

方法一:暴力法。Time:O(n^2),Space:O(1)

方法二:利用一个哈希表来保存另外一个数字以及数字出现的下标。Time:O(n),Space:O(n)

二:完整代码示例 (C,C++、Java、python)

方法一C:

int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
    int i = 0;
    int j = 0;
    int* result = (int*)malloc(2*sizeof(int));
    for (i = 0; i < numsSize; i++) {
        for (j = i + 1; j < numsSize; j++) {
            if (nums[i] + nums[j] == target) {
                result[0] = i;
                result[1] = j;
                *returnSize = 2;
                break;
            }
        }
    }

    return result;
}

方法二C:

 

 

方法一C++:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) 
    {
        vector<int> result = {-1,-1};
        if (nums.size() == 0) return result;

        for (int i = 0; i < nums.size(); i++)
        {
            for (int j = i + 1; j < nums.size(); j++)
            {
                if (nums[i] + nums[j] == target)
                {
                    result[0] = i;
                    result[1] = j;
                    break;
                }
            }
        }

        return result;
    }
};

方法二C++:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) 
    {
        vector<int> result = {-1,-1};
        if (nums.size() == 0) return result;
        map<int, int> hash_map;

        for (int i = 0; i < nums.size(); i++)
        {
            int another = target - nums[i];
            if (hash_map.count(another) > 0)
            {
                result[0] = hash_map[another];
                result[1] = i;
                break;
            }
            hash_map[nums[i]] = i;
        }

        return result;
    }
};

方法一Java:

class Solution {
        public int[] twoSum(int[] nums, int target)
        {
                if(nums==null || nums.length==0) return new int[]{-1,-1};

                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 new int[]{-1,-1};
        }
    }

方法二Java:

class Solution {
        public int[] twoSum(int[] nums, int target)
        {
               if(nums==null || nums.length==0) return new int[]{-1,-1};
               Map<Integer,Integer> hash_map=new HashMap<>();
               
               for(int i=0;i<nums.length;i++)
               {
                   int another=target-nums[i];
                   if(hash_map.containsKey(another))
                   {
                       return new int[]{hash_map.get(another),i};
                   }
                   hash_map.put(nums[i],i);
               }
               
               return new int[]{-1,-1};
        }
    }

方法一python:

from typing import List
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        length=len(nums)
        for i in range(0,length):
            for j in range(i+1,length):
                if nums[i]+nums[j]==target:
                    return [i,j]
        return [-1,-1]

方法二python(写法一):

from typing import List
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        hash_map={}
        for idx,value in enumerate(nums):
            if value in hash_map:
                return [hash_map[value],idx]
            else:
                hash_map[target-value]=idx

 

方法二python(写法二):

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        if nums is None:return [-1,-1]
        length=len(nums)
        hash_map={}

        for idx,value in enumerate(nums):
            another=target-value
            if another in hash_map:
                return [hash_map.get(another),idx]
            hash_map[value]=idx
        return [-1,-1]

 

方法二python(写法三):

from typing import List
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        if nums is None : return [-1,-1]
        hash_map={}
        length=len(nums)
        for i in range(0,length):
            another=target-nums[i]
            if hash_map.get(another) is not None:
                return [i,hash_map[another]]
            hash_map[nums[i]]=i
        return [-1,-1]
 

 

from typing import List

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        hash_map={}
        n = len(nums)
        for i in range(0,n):
            another = target - nums[i]
            if hash_map.get(another) is not None:
                return [i,hash_map.get(another)]
            hash_map[nums[i]]=i
        return [-1,-1]

 

posted @ 2020-03-05 17:03  repinkply  阅读(249)  评论(0)    收藏  举报