leetcode刷题记录-Two Sum

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.

解题思路:

利用target减去数组中每个数得到数组的差值数组,以原题中的例子为例:{2,7,11,15}对应的差值数组为{7,2,-1,,6},然后遍历原数组,若差值数组中也包含这个数且对应索引不相等,则返回原数组该值索引与差值数组中的索引。为了让时间复杂度为O(n),差值数据需用哈希map的方式保存。

解题源码:

 1 class Solution {
 2 public:
 3     vector<int> twoSum(vector<int>& nums, int target) {
 4         map<int, int> finder;
 5         int index1 = -1;
 6         int index2 = -1;
 7         for(int i=0; i<nums.size(); ++i){
 8             int _num = target - nums[i];
 9             finder.insert(map<int, int>::value_type(_num, i));
10         }
11         for(int i=0; i<nums.size(); ++i){
12             map<int, int>::iterator itr;
13             itr = finder.find(nums[i]);
14             if (itr!=finder.end()){
15                 int j = finder[nums[i]];
16                 if (i!=j){
17                     index1 = min(i, j);
18                     index2 = max(i, j);
19                     break;
20                 }
21             }
22         }
23         vector<int> rst;
24         rst.push_back(index1+1);
25         rst.push_back(index2+1);
26         return rst;
27     }
28 };
时间: 2024-08-23 06:53:11

leetcode刷题记录-Two Sum的相关文章

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

leetcode刷题记录(2)

301. Remove Invalid Parentheses Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples: "()())()&q

Leetcode刷题记录[python]——344 Reverse String

一.前言 不是计算机专业出身,却有一颗程序猿的心. 昨日开始leetcode第一次刷题,选择了菜鸟方式,从AC率最高且难度为Easy的题开始,不管题是简单还是难,都想做个记录,既是方便以后回顾,又是以此作为一个激励,督促自己每天都能有所进步. 二.题344 Reverse String Write a function that takes a string as input and returns the string reversed. class Solution(object): def

LeetCode刷题记录【001】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,

LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现

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]

Leetcode刷题记录[python]——349 Intersection of Two Arrays

一.前言 做了两题才慢慢摸清了leetcode的操作. 二.题349 Intersection of Two Arrays Given two arrays, write a function to compute their intersection. class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int]

Leetcode刷题记录[python]——283 Move Zeroes

一.前言 题是上周五做的,开始思路有点问题,考虑不全,导致submit了3次才AC. 二.题283 Move Zeroes Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after call

Leetcode刷题记录[python]——561 Array Partition I

一.前言 二.题561 Array Partition I Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible. Example 1: Inp

leetcode刷题记录(JAVA&amp;Python)

---恢复内容开始--- --题目导航见页面左上角的悬浮框#目录导航#-- 相似题型: 1.1 twosum两数之和   2.2 3Sum三数之和 一.简单 1.1 twosum两数之和 原题: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 实例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 暴力解