1.Two Sum

1 问题:

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].
译: 给出一个整数数组, 找出其中两个数相加等于target的 对应下标。 假设数组肯定存在两个元素满足相加等于target的条件,并且这两个元素 下标不一样。


2 方法:

    方法一: 时间复杂度O(n^2).  差评。  [ me ]


#include<stdio.h>
#include<stdlib.h>
int* twoSum(int* nums, int numsSize, int target);

int main()
{
    int a[4] = {2,5,7,12};
    int numSize = 4; 
    int target = 9;
   
    int *result = twoSum(a, numSize, target);
    if(NULL != result)
    {
        printf("a:%d\n", result[0]);
        printf("b:%d\n", result[1]);
        free(result);
    }
    return 0;
}

/**
 *  * Note: The returned array must be malloced, assume caller calls free().
 *   */
int* twoSum(int* nums, int numsSize, int target) {
    if (NULL == nums) 
    {
        printf("no 1");
        return NULL;
    }
    int* result = (int *)malloc(sizeof(int)*2);
    if (NULL == result)
    {
        printf("no 2");
        return NULL;
    }
    int i=0, j = 0;
    for(;i < numsSize; i++)
        for(j=i+1; j< numsSize; j++)
        {
            printf("%d,%d\n", nums[i], nums[j]);
            if (target == nums[i] + nums[j])
            {
                result[0] = i;
                result[1] = j;
                return result;
            }
        }
    return NULL;
}


   方法二:时间复杂度O(n),  好评。

vector<int> twoSum(vector<int> &numbers, int target)  
{     
    //Key is the number and value is its index in the vector.  
    unordered_map<int, int> hash;  
    vector<int> result;  
    for (int i = 0; i < numbers.size(); i++)   
    {  
        int numberToFind = target - numbers[i];            
        //if numberToFind is found in map, return them  
        if (hash.find(numberToFind) != hash.end())   
        {                      
            result.push_back(hash[numberToFind]);  
            result.push_back(i);
            return result;
        }           
        //number was not found. Put it in the map.  
        hash[numbers[i]] = i; 
    } 
    return result;   
}



3. unordered_map的find方法: 

Return value

An iterator to the element, if the specified key value is found, or  unordered_map::end if the specified key is not found in the container.

Member types  iterator and  const_iterator are  forward iterator types.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// unordered_map::find
#include <iostream>
#include <string>
#include <unordered_map>

int main ()
{
  std::unordered_map<std::string,double> mymap = {
     {"mom",5.4},
     {"dad",6.1},
     {"bro",5.9} };

  std::string input;
  std::cout << "who? ";
  getline (std::cin,input);

  std::unordered_map<std::string,double>::const_iterator got = mymap.find (input);

  if ( got == mymap.end() )
    std::cout << "not found";
  else
    std::cout << got->first << " is " << got->second;

  std::cout << std::endl;

  return 0;
}
详情见: http://www.cplusplus.com/reference/unordered_map/unordered_map/find/


4. 延伸:

 关于 unordered_map,又叫hash_map,实际是“hashtable” 【与redis 的字典结构一样】。 如图:



详情见: http://blog.csdn.net/blues1021/article/details/45054159













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值