• 【LeetCode】1. Two Sum


    1. Two Sum
    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.

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

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


    看到这个题首先想到的是保存二重循环,先保存一个值,然后去找去找target-val

     1 int* twoSum(int* nums, int numsSize, int target) {
     2     int  i,j;
     3     int bi=-1,bj=-1,tmp;
     4     for(i=0;i<numsSize-1;i++)
     5     {
     6         bi=i;
     7         tmp=target-nums[i];
     8         for(j=i+1;j<numsSize;j++)
     9         {
    10             if(tmp==nums[j])
    11                 {
    12                     bj=j;
    13                     break;
    14                 }
    15         }
    16         if(bj!=-1)
    17             break;
    18     }
    19     int *ptmp=malloc(sizeof(int)*2);
    20     ptmp[0]=bi;
    21     ptmp[1]=bj;
    22     return ptmp;
    23 }

     方法二:O(n)

    每遍历一个数字val时候,首先查找在hash表中有没有target-val。

    1.如果有的话,break,然后找到target-val的位置,返回;

    2.如果没有找到target-val的话,在hash中记录此数字。

     1 /**
     2  * Note: The returned array must be malloced, assume caller calls free().
     3  */
     4 int* twoSum(int* nums, int numsSize, int target) {
     5     int  i,j;
     6     int hash[10000]={0};    //存储正值
     7     int hashp[10000]={0};    //存储负值
     8     int couple;
     9     for(i=0;i<numsSize;i++)
    10     {
    11         couple=target-nums[i];
    12         if(couple>0)
    13             {
    14                 if(hash[couple>>5]&(1<<(couple&0x1f)))
    15                 break;
    16             }
    17         else
    18             {
    19                 if(hashp[(-couple)>>5]&(1<<((-couple)&0x1f)))
    20                 break;
    21             }
    22         if(nums[i]>0)
    23             hash[nums[i]>>5]|=(1<<(nums[i]&0x1f));
    24         else
    25             hashp[(-nums[i])>>5]|=(1<<((-nums[i])&0x1f));            
    26     }
    27     for(j=0;j<i;j++)
    28         if(couple==nums[j])
    29             break;
    30     int*n=malloc(sizeof(int)*2);
    31     n[0]=j;
    32     n[1]=i;
    33     return n;
    34 }

     python版:

     1 class Solution:
     2     def twoSum(self, nums, target):
     3         """
     4         :type nums: List[int]
     5         :type target: int
     6         :rtype: List[int]
     7         """
     8         flag = set()
     9         res = []
    10         for i, item in enumerate(nums):
    11             if target-item in flag:
    12                 res.append(i)
    13                 break
    14             else:
    15                 flag.add(item)
    16         if res:
    17             res.append(nums.index(target-item))
    18         return res
    19         
     1 class Solution:
     2     def twoSum(self, nums, target):
     3         if len(nums)<=1:
     4             return False
     5         buff = {}
     6         for i,v in enumerate(nums):
     7             if v in buff:
     8                 return [buff[v], i]
     9             else:
    10                 buff[target-v]=i
  • 相关阅读:
    [Python] Python2 、Python3 urllib 模块对应关系
    [Python] Mac pip安装的模块包路径以及常规python路径
    git 使用详解
    版本控制工具简介
    python基础练习题(二)
    python简介,安装
    python基础练习题(一)
    python练习题之面向对象(三)
    python之input函数,if,else条件语句使用的练习题(一)
    C++ 静态变量
  • 原文地址:https://www.cnblogs.com/fcyworld/p/6146445.html
Copyright © 2020-2023  润新知