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

来源:互联网 发布:excel两列重复数据筛选 编辑:程序博客网 时间:2024/06/02 03:42

题目

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<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);                }            }        }        return res;    }};
0 0
原创粉丝点击