[leetcode笔记] Two-sum

注册了leetcode账号,一共有150道题,准备有时间就刷一刷,许久没有编程了,C++早已生疏。

目前leetcode也支持Python了,俺们还是先练练C++。先找个简单的开始,随便挑了一个AC率稍微高点的题"Two Sum"。

Two Sum

https://oj.leetcode.com/problems/two-sum/

Total Accepted: 25599 Total Submissions: 140441

 

问题描述:

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)第一次Submit

先用最土的办法试试:

 

 1 class Solution {
 2 public:
 3     vector<int> twoSum_01_timeout(vector<int> &numbers, int target) {
 4 
 5         int a = 0, b = 0;
 6         bool breakFlag = false;
 7         for(a = 0; a < (int)numbers.size()-1; a++)
 8         {
 9             for(b=a+1; b< (int)numbers.size(); b++)
10             {
11                 if(numbers[a] + numbers[b] == target)
12                 {
13                     breakFlag = true;
14                     break;
15                 }
16             }
17             if(breakFlag==true){ break; }
18         }
19 
20         vector<int> rv(2);
21         rv[0] = a+1;
22         rv[1] = b+1;
23         return rv;
24     }
25 }

 

不出意外,Timeout!哪能这么简单!

 

(2)第二次Submit

改进一下,先搞个排序试试,复习了一下STL sort方法用法。

 

 1 bool lesspair(const pair<int,int>& p1, const pair<int,int>& p2 )
 2 {
 3     return p1.first < p2.first;
 4 }
 5 
 6 void print_pair_vector(vector<pair<int,int>>& ns)
 7 {
 8     int i = 0;
 9     for(i=0; i<static_cast<int>(ns.size()); i++)
10     {
11         cout<< "(" << ns[i].first << ", " << ns[i].second << ")  ";
12     }
13 }
14 
15 class Solution {
16 public:
17     vector<int> twoSum(vector<int> &numbers, int target) {
18 
19         vector<pair<int,int>> ns;
20         int i = 0;
21         for(i = 0; i<static_cast<int>(numbers.size()); i++)
22         {
23             pair<int,int> p(numbers[i], i+1);
24             ns.push_back(p);
25         }
26 
27         //print_pair_vector(ns);
28         sort(ns.begin(), ns.end(), lesspair);
29         //print_pair_vector(ns);
30 
31         int a = 0, b = 0;
32         bool breakFlag = false;
33         for(a = 0; a < (int)ns.size()-1; a++)
34         {
35             for(b=a+1; b< (int)ns.size(); b++)
36             {
37                 if(ns[a].first + ns[b].first == target)
38                 {
39                     breakFlag = true;
40                     break;
41                 }
42             }
43             if(breakFlag==true){ break; }
44         }
45 
46 
47         vector<int> rv(2);
48         if ( ns[a].second < ns[b].second )
49         {
50             rv[0] = ns[a].second;
51             rv[1] = ns[b].second;
52         }
53         else
54         {
55             rv[0] = ns[b].second;
56             rv[1] = ns[a].second;
57         }
58         return rv;
59     }
60 };

 

刚开始没搞对返回值顺序,稍作修改再次提交。Accepted!不会吧,这就过了?

感觉俺的方法还是有点土啊![汗]

转载于:https://www.cnblogs.com/lequ/p/3863497.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值