|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目描述:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
tips:同时提供c++与python两种语言
- class Solution {
- public:
- vector<int> twoSum(vector<int>& nums, int target) {
- for (int i = 0; i < nums.size(); i++)
- {
- for (int j = i + 1; j < nums.size(); j++)
- {
- if (nums[j] == target - nums[i])
- {
- vector<int> lst1;
- lst1.push_back(i);
- lst1.push_back(j);
- return lst1;
- }
- }
- }
- }
- };
复制代码
- class Solution:
- def twoSum(self, nums, target):
- """
- :type nums: List[int]
- :type target: int
- :rtype: List[int]
- """
- for index,num in enumerate(nums):
- s=target-num
- if s in nums and index!=nums.index(s):
- return [index,nums.index(s)]
- return -1
复制代码 |
|