LeetCode.001 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

题意

从一个数组中找到两个数,使这两个数之和等于给定的目标数。   返回这两个数在数组中的位置

思路

1.将原数组复制在temp中,并排序

2.从排序数组的头和尾开始扫描:如果head+tail<target,那么head++,接着扫描;

如果head+tail>target,那么tail--,接着扫描;直到head+tail=target。

3.从原数组中找到temp[head] 和 temp[tail]。

 1 //Runtime: 16 ms
 2 class Solution {
 3 public:
 4     vector<int> twoSum(vector<int>& nums, int target) {
 5     vector<int> temp = nums;
 6     sort(temp.begin(),temp.end());
 7     int head = 0;
 8     int tail = temp.size()-1;
 9     while(temp[head]+temp[tail] != target)
10     {
11         if(temp[head]+temp[tail] > target)
12             tail--;
13         else
14             head++;
15     }
16     vector<int> array(2,0);
17     for(int i=0;i<temp.size();i++)
18     {
19         if(nums[i] == temp[head] || nums[i] == temp[tail])
20             if(array[0] == 0)
21                 array[0] = i+1;
22             else
23                 array[1] = i+1;
24     }
25     return array;
26     }
27 };
时间: 2024-08-06 03:15:08

LeetCode.001 Two Sum的相关文章

[LeetCode] 001. Two Sum (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 001.Two_Sum (Medium) 链接: 题目:https://oj.leetcode.com/problems/two-sum/ 代码(github):https://github.com/illuz/leetcode 题意: 一个数组中两个位置上的数的和恰为 target,求这两个位置. 分析: 暴力找

LeetCode #001# Two Sum详解(js描述)

索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 问题描述:https://leetcode.com/problems/two-sum/ 思路1:暴力搜索 一个很自然的想法就是暴力搜索了.根据排列组合原理,列举Cn取2对数字,逐对进行判断,效率是O(n^2-1/2n),代码如下: var twoSum = function(nums, target) { for (let i = 0; i != nums.length; ++i) { for (let j = i

LeetCode 001 Two Sum - 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]

Java for LeetCode 001 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

LeetCode-Algorithms #001 Two Sum, Database #175 Combine Two Tables

最近两周一直感觉学习比较松懈, 打算加大一点强度, 从今天开始, 希望能在每天正常进度完成后在LeetCode上选一两题写一写, 同时学习一下高手的做法. LeetCode - Algorithms #001 Two Sum 给定一个整数数组, 找出其中两个元素, 使其和等于目标值, 返回这两个元素在原数组中的索引组成的数组. 可以假设有且只有一组正解, 且每个元素只能使用一次. 我的思路: 其实想不到什么好的方法, 遍历就好, 非常素朴的回答: 1 class Solution { 2 pub

LeetCode --- 1. Two Sum

题目链接: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. Plea

LeetCode:Range Sum Query - Immutable - 数组指定区间内的元素和

1.题目名称 Range Sum Query(数组指定区间内的元素和) 2.题目地址 https://leetcode.com/problems/range-sum-query-immutable/ 3.题目内容 英文:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. 中文:给定一个数组nums,求出索引i和j之间元素的和,i一定是小于或等于j

【Leetcode】Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路:与[Leetcode]Path Sum 不同

LeetCode:Path Sum - 树的根节点到叶节点的数字之和

1.题目名称 Path Sum(树的根节点到叶节点的数字之和) 2.题目地址 https://leetcode.com/problems/path-sum/ 3.题目内容 英文:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. 中文:给定一颗二叉树,如