leetcode 1 two sum

Question:Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
    }

本題比較簡單,用hashmap即可,講一下容易出錯(cuò)的地方:

trap: 給的序列可能是重復(fù)的,所以我選擇用兩個(gè)map

//
//  main.cpp
//  leetcode
//
//  Created by YangKi on 15/11/11.
//  Copyright ? 2015年 YangKi. All rights reserved.
//

#include<cstdlib>
#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> map;
        unordered_map<int, int> map2;
        vector<int>ans;
        int size=nums.size();
        for(int i=0; i<size; i++)
        {
            if(map.find(nums[i]) == map.end())
            {
                map[ nums[i] ]= (i+1);
            }
            else//duplicate
            {
                map2[ nums[i] ]=(i+1);
            }
        }
        
        for(int i=0; i<size; i++)
        {
            if( map.find(target-nums[i]) != map.end() )//success
            {
                if(nums[i] == target-nums[i])
                {
                    if(map2.find(nums[i]) != map2.end() )
                    {
                        ans.push_back(min(map[nums[i]], map2[nums[i]]));
                        ans.push_back(max(map[nums[i]], map2[nums[i]]));
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    ans.push_back(min(map[nums[i]], map[target-nums[i]]));
                    ans.push_back(max(map[nums[i]], map[target-nums[i]]));
                }
                break;
            }
        }
        
        return ans;
    }
};

int main()
{
    Solution *s=new Solution();
    vector<int>v={2,7,11,15};
    vector<int>vv=s->twoSum(v, 9);
    printf("%d %d\n", vv[0], vv[1]);
    return 0;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 注:之前在其他平臺(tái)的文章與記錄慢慢也都放這邊吧,權(quán)當(dāng)資料遷移與備份。如下: LeetCode,最近才知道有這么好的...
    way菜畦閱讀 3,699評(píng)論 5 7
  • ViewPager在開發(fā)中的使用頻率非常的高,所以在此做個(gè)總結(jié)。 一、簡介 1.ViewPager的簡介和作用Vi...
    萬戶猴閱讀 308評(píng)論 0 1
  • 現(xiàn)在生活條件好,大家雞鴨魚肉吃得太多,很多朋友都知道,過度精致飲食會(huì)導(dǎo)致現(xiàn)代多種疾病。所以會(huì)考慮多吃五谷雜糧,換換...
    楊靜相伴要你好看閱讀 465評(píng)論 0 1
  • 六點(diǎn)鐘,爺爺正在爬行墊上逗孩子,這時(shí)門開了,小濤下班回家了。他看到父親,一邊換鞋,一邊笑著說,"爸,你過來啦!"父...
    慢慢阿維娃閱讀 335評(píng)論 9 4
  • 首先我們先來了解什么是社交資產(chǎn)? 舉個(gè)大家都耳熟能詳?shù)睦樱合竦玫骄褪沁壿嬎季S羅胖他本身就有強(qiáng)大的粉絲量,他把他...
    Muriel小王子閱讀 236評(píng)論 1 5

友情鏈接更多精彩內(nèi)容