力扣(LeetCode)试题1-两数之和 C++代码

这种“暴力匹配”算法还是慢,再学习新的方法之后,再来更新这部分代码吧。

 

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 
 5 class Solution {
 6 public:
 7 public:
 8     vector<int> twoSum(vector<int>& nums, int target) {
 9         vector<int> index;
10         for (int i = 0; i<nums.size(); i++){
11             for (int j = i + 1; j<nums.size(); j++){
12                 if (nums[i] + nums[j] == target){
13                     index.push_back(i);
14                     index.push_back(j);
15                 }
16                 else
17                     continue;
18             }
19         }
20         return index;
21     }
22 };
23 int main(){
24     Solution sol;
25     int n;
26     vector<int> out;
27     vector<int> nums = { 3,2,3 };
28     int target = 6;
29     out = sol.twoSum(nums, target);
30     for (auto i:out)
31         cout << i << endl;
32     cin >> n;
33     return 0;
34 }

 

 

posted @ 2020-06-30 13:16  ZyLin-ux  阅读(372)  评论(0编辑  收藏  举报