LeetCode- 1. Two Sum - 思路详解-C++

题目

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.

翻译

给定义个数组,找到数组中的两个值之和为目标值,返回这两个数的下标,假设每一个数组都有一组特定的解。

思路

暴力法,两次循环

代码

class Solution {
public:
    vector twoSum(vector& nums, int target) {
        vector 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);
                }
            }
        }
        return res;
    }
};

你可能感兴趣的:(LeetCode,C++,数组,算法)