0
点赞
收藏
分享

微信扫一扫

1. Two Sum C++

小猪肥 2022-02-02 阅读 59
c++leetcode

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Answer:

class Solution {

public:

    vector<int> twoSum(vector<int>& nums, int target) {

        vector<int>a;

        for(int i=0;i<nums.size();i++)

            for(int j=1;j<nums.size();j++){

                if(nums[i]+nums[j]==target&&i!=j){

                    a.push_back(i);

                    a.push_back(j);

                    return a;

                }

            }

            return a;

    }

};

举报

相关推荐

0 条评论