Leetcode刷题录之Two Sum

题意大概是给出一个数列num,和一个目标数target,然后要找出数列中的两个数,使得这两个数之和等于目标数,输出这两个数的下标值(从1开始算)。

  一个比较暴力的方法是用一个二重循环直接遍历序列,在第一重循环中找到a,在第二重循环中找到b,使得a+b=target,这种做法的时间复杂度是O(n^2),

提交时提示超时。

  改进方法,先对数列num复制一个副本,然后对副本进行排序。在一重循环中找到a,接着对这个有序的副本进行二分查找,找到b=
target-a,二分查找的

时间复杂度是O(logn),故整个过程的时间复杂度是O(nlogn),降低了时间复杂度,提交通过。

附上C++代码:


 1     class Solution {
2 public:
3 int binarySearch(vector<int> &numbers,int key,int left,int right)
4 {
5 if(left > right) {
6 return -1;
7 }
8 int mid = (left + right)/2;
9 if(numbers[mid] == key) {
10 return mid;
11 }
12 if(numbers[mid] < key) {
13 return binarySearch(numbers,key,mid+1,right);
14 }
15 if(numbers[mid] > key) {
16 return binarySearch(numbers,key,left,mid-1);
17 }
18 }
19 vector<int> twoSum(vector<int> &numbers, int target) {
20 vector<int> copy = numbers;
21 vector<int> index;
22 sort(copy.begin(),copy.end());
23 int n1,n2;
24 for(int i = 0; i < copy.size(); i++) {
25 int key = target - copy[i];
26 int mid = binarySearch(copy,key,0,copy.size());
27 if( mid != -1) {

28 n1 = copy[i];
29 n2 = copy[mid];
30 }
31 }
32 for(int i = 0,j = 0;i < numbers.size() && j <= 2;i++) {
33 if(numbers[i] == n1) {
34 if(index.empty() || (!index.empty() && index[0] != i+1) ) {
35 index.push_back(i+1);
36 j++;
37 }
38 }
39 if(numbers[i] == n2) {
40 if(index.empty() || (!index.empty() && index[0] != i+1) ) {
41 index.push_back(i+1);
42 j++;
43 }
44 }
45 }
46
47 if(index[0] > index[1]) {
48 int temp = index[0];
49 index[0] = index[1];
50 index[1] = temp;
51 }
52 //cout<<index[0]<<" " <<index[1]<<endl;
53 return index;
54 }
55
56
57 };

Leetcode刷题录之Two Sum,布布扣,bubuko.com

时间: 2024-10-17 19:24:56

Leetcode刷题录之Two Sum的相关文章

【leetcode刷题笔记】Path Sum

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. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t

leetcode 刷题录

11. 盛最多水的容器 class Solution { public: int maxArea(vector<int>& height) { vector<int> & v =height; int l =0,r=v.size()-1; int res = 0; while(l<r) { int lval = v[l]; int rval = v[r]; int minval = min(lval,rval); res =max(res,minval*(r-

【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 刷题之路 66 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] ] 给定一个二叉树和数字sum,输出二叉树中从根节点到

LeetCode刷题(一):Two Sum

今天开始在LeetCode刷题,第一题为"两数之和(Two Sum)",整体来讲这一题难度是比较低的,但还是在这个过程中遇到一些问题,以下主要记录出现的一些问题. 这个题目比较容易想到的方法便是穷举了,很暴力但也很直接,需要注意的一点便是向量库Vector,自己也是前一阵子开始学数据结构才知道有这个库(有的也称为容器),定义计算数组长度用的方法是size()而不是length(),官方解答给的是length(),不知道是不是没有注意到还是因为他用的代码是Java(本人不了解Java),

【leetcode刷题笔记】4Sum

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending order.

【leetcode刷题笔记】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【leetcode刷题笔记】Gas Station

There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an e

【leetcode刷题笔记】3Sum

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The solut