博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1. Two Sum(C++)
阅读量:4612 次
发布时间:2019-06-09

本文共 727 字,大约阅读时间需要 2 分钟。

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]

 

答案:

class Solution {

public:

  vector<int> twoSum(vector<int>& nums, int target) {
    vector<int> myarray;
    for(int i=0;i<nums.size()-1;i++){
      for(int j=i+1;j<nums.size();j++){
        if((nums[i]+nums[j])==target){
          myarray.push_back(i);
          myarray.push_back(j);
          break;
        }
      }
    }
    return myarray;
  }
};

 

转载于:https://www.cnblogs.com/devin-guwz/p/6425583.html

你可能感兴趣的文章
Mac配置Fiddler抓包工具
查看>>
转:Java并发集合
查看>>
Word截图PNG,并压缩图片大小
查看>>
Python项目对接CAS方案
查看>>
mysql产生随机数
查看>>
编程风格
查看>>
熟悉常用的Linux命令
查看>>
易之 - 我是个大师(2014年3月6日)
查看>>
Delphi中窗体的事件
查看>>
file_get_contents()获取https出现这个错误Unable to find the wrapper “https” – did
查看>>
linux vi编辑器
查看>>
js树形结构-----(BST)二叉树增删查
查看>>
contract
查看>>
FJUT ACM 1899 Largest Rectangle in a Histogram
查看>>
如何删除xcode项目中不再使用的图片资源
查看>>
编写用例文档
查看>>
解决WPF两个图片控件显示相同图片因线程占用,其中一个显示不全的问题
查看>>
寻觅Azure上的Athena和BigQuery (二):神奇的PolyBase
查看>>
编程题练习
查看>>
mac os安装vim74
查看>>