首页 > 其他 > 详细

LeetCode-1

时间:2019-03-31 23:42:20      阅读:154      评论:0      收藏:0      [点我收藏+]

2019/03/31

今天开始在LeetCode上刷题,第一道只会普通的解法,先记录下来,以后有新的想法再改进。

时间复杂度O(n2),空间复杂度O(1)

 1 vector<int> twoSum(vector<int>& nums, int target) {
 2         vector<int> returnVector;
 3                 
 4         for (vector<int>::size_type i = 0; i < nums.size(); ++i)
 5         {
 6             for (vector<int>::size_type j = i + 1; j < nums.size(); ++j)
 7             {
 8                 if (target == nums[i] + nums[j])
 9                 {
10                     returnVector.push_back(i);
11                     returnVector.push_back(j);
12                     return returnVector;
13                 }
14             }   
15         }
16         
17         return returnVector;
18     }

执行效果如下:

技术分享图片

LeetCode-1

原文:https://www.cnblogs.com/zpchya/p/10633907.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!