Two Sum<\/a><\/h2> \n Given an array of integers, return indices of the two numbers such that they add up to a specific target.\n\nYou may assume that each input would have exactly one solution, and you may not use the same element twice.<\/code><\/pre> \n Example:<\/h3> \n Given nums = [2, 7, 11, 15], target = 9,\n\nBecause nums[0] + nums[1] = 2 + 7 = 9,\nreturn [0, 1].<\/code><\/pre> \n Code<\/h3> \n \/\/\n\/\/ main.cpp\n\/\/ STL_Vector\n\/\/\n\/\/ Created by mac on 2019\/7\/13.\n\/\/ Copyright © 2019 mac. All rights reserved.\n\/\/\n\n#include <iostream>\n#include <vector>\n#include <algorithm>\n\n\nusing namespace std;\n\nclass Solution{\npublic:\n vector<int> twoSum(vector<int>& nums,int target){\n vector<int> result;\n for (int i=0; i<nums.size()-1; i++) {\n for (int j=i+1; j<nums.size(); j++) {\n if (nums[i]!=nums[j]) {\n if (nums[i]+nums[j]==target) {\n result.push_back(i);\n result.push_back(j);\n }\n }\n }\n }\n return result;\n }\n};\n\nint main(int argc, const char * argv[]) {\n \/\/ insert code here...\n Solution solu;\n vector<int> nums={2, 7, 11, 15};\n \/\/int nums [4]= {2, 7, 11, 15};\n int target=9;\n \n \n for (int i=0; i<solu.twoSum(nums, target).size();i++) {\n cout<<solu.twoSum(nums, target)[i]<<endl;\n }\n return 0;\n}\n<\/code><\/pre> \n \u8fd0\u884c\u7ed3\u679c<\/h3> \n 0\n1\nProgram ended with exit code: 0<\/code><\/pre> \n \u53c2\u8003\u6587\u732e<\/h3> \n \n - https:\/\/leetcode.com\/problems\/two-sum\/<\/a><\/li> \n <\/ul> \n<\/div>","orderid":"0","title":"Two Sum","smalltitle":"","mid":"0","fname":"c++\u7f16\u7a0b\u57fa\u7840","special_id":"0","bak_id":"0","info":"0","hits":"57","pages":"1","comments":"0","posttime":"2019-07-14 18:09:48","list":"1563098988","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Two<\/A> Sum<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"47.106.78.186","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Two Sum","lastview":"1713492136","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}
Given an array of integers, return indices of the two numbers such that they add up to a specific target.\n\nYou may assume that each input would have exactly one solution, and you may not use the same element twice.<\/code><\/pre> \n Example:<\/h3> \n Given nums = [2, 7, 11, 15], target = 9,\n\nBecause nums[0] + nums[1] = 2 + 7 = 9,\nreturn [0, 1].<\/code><\/pre> \n Code<\/h3> \n \/\/\n\/\/ main.cpp\n\/\/ STL_Vector\n\/\/\n\/\/ Created by mac on 2019\/7\/13.\n\/\/ Copyright © 2019 mac. All rights reserved.\n\/\/\n\n#include <iostream>\n#include <vector>\n#include <algorithm>\n\n\nusing namespace std;\n\nclass Solution{\npublic:\n vector<int> twoSum(vector<int>& nums,int target){\n vector<int> result;\n for (int i=0; i<nums.size()-1; i++) {\n for (int j=i+1; j<nums.size(); j++) {\n if (nums[i]!=nums[j]) {\n if (nums[i]+nums[j]==target) {\n result.push_back(i);\n result.push_back(j);\n }\n }\n }\n }\n return result;\n }\n};\n\nint main(int argc, const char * argv[]) {\n \/\/ insert code here...\n Solution solu;\n vector<int> nums={2, 7, 11, 15};\n \/\/int nums [4]= {2, 7, 11, 15};\n int target=9;\n \n \n for (int i=0; i<solu.twoSum(nums, target).size();i++) {\n cout<<solu.twoSum(nums, target)[i]<<endl;\n }\n return 0;\n}\n<\/code><\/pre> \n \u8fd0\u884c\u7ed3\u679c<\/h3> \n 0\n1\nProgram ended with exit code: 0<\/code><\/pre> \n \u53c2\u8003\u6587\u732e<\/h3> \n \n - https:\/\/leetcode.com\/problems\/two-sum\/<\/a><\/li> \n <\/ul> \n<\/div>","orderid":"0","title":"Two Sum","smalltitle":"","mid":"0","fname":"c++\u7f16\u7a0b\u57fa\u7840","special_id":"0","bak_id":"0","info":"0","hits":"57","pages":"1","comments":"0","posttime":"2019-07-14 18:09:48","list":"1563098988","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Two<\/A> Sum<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"47.106.78.186","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Two Sum","lastview":"1713492136","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}