1. Two Sum

原题链接

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.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

翻译:给定一个数组,和一个目标值,在原数组中找出两个值相加为目标值,答案唯一。

思路:两重循环暴力过。
AC代码:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> res;
        for(int i=0;i<nums.size();i++)
        {
            for(int j=i+1;j<nums.size();j++)
            {
                if(nums[i]+nums[j]==target)
                {
                    res.push_back(i);
                    res.push_back(j);
                    break;
                }
            }
        }
        return res;
    }
};
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/rockywallet/article/details/79973658
个人分类: LeetCode
上一篇561. Array Partition I
下一篇378. Kth Smallest Element in a Sorted Matrix
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭