题目

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.

样例:

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

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

答案:

简单遍历vector就行了,size*size复杂度,本来想先排序,再遍历,但是排序需要记录排序前和排序后索引的对应关系。

代码:

 #include <algorithm>

 class Solution {
private:
static bool cmp(const int &a, const int &b)
{
return a < b;
} public:
vector<int> twoSum(vector<int>& nums, int target) {
// std::sort(nums.begin(),nums.end(),cmp); int i,j;
int size = nums.size();
int rest;
vector<int> ans; for(i = ; i < size; ++ i)
{
rest = target - nums[i];
for(j = i + ;j < size; ++ j)
{
if(rest == nums[j])
{
ans.push_back(i);
ans.push_back(j);
return ans;
}
}
}
}
};

leetcode-【简单题】Two Sum的更多相关文章

  1. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  2. 这样leetcode简单题都更完了

    这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...

  3. 乘风破浪:LeetCode真题_039_Combination Sum

    乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...

  4. leetcode简单题6

    今天的华师 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...

  5. LeetCode算法题-Two Sum IV - Input is a BST(Java实现)

    这是悦乐书的第280次更新,第296篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第148题(顺位题号是653).给定二进制搜索树和目标数,如果BST中存在两个元素,使得 ...

  6. LeetCode算法题-Path Sum III(Java实现)

    这是悦乐书的第227次更新 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第94题(顺位题号是437).您将获得一个二叉树,其中每个节点都包含一个整数值.找到与给定值相加的路径数 ...

  7. LeetCode算法题-Range Sum Query Immutable(Java实现)

    这是悦乐书的第204次更新,第214篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第70题(顺位题号是303).给定整数数组nums,找到索引i和j(i≤j)之间的元素之 ...

  8. LeetCode算法题-Two Sum II - Input array is sorted

    这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...

  9. LeetCode算法题-Path Sum(Java实现)

    这是悦乐书的第169次更新,第171篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第28题(顺位题号是112).给定二叉树和整数sum,确定树是否具有根到叶路径,使得沿路 ...

  10. Go: LeetCode简单题,简单做(sort.Search)

    前言 正值端午佳节,LeetCode也很懂.这两天都是简单题,早点做完去包粽子. 故事从一道简单题说起 第一个错误的版本 简单题 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最 ...

随机推荐

  1. ACE - Reactor实现I/O,Dispatch,Service三层完整服务器(完结)

    框架描述 服务器层次: I/O层:对应具体的文件描述符处理,对应ACE中的handle. Dispatch层:事件分发,将I/O事件分发到对应绑定的处理队列等待业务处理,对应ACE中的Event_ha ...

  2. Apache2.2与php5.17 mysql配置

    php5.217应该用线程安全搬,不然各种无语的Apache打不开,PHPInfo没有Mysql的信息,记得把php.ini放入系统盘Windows目录下,Win764位的libmysql.dll也放 ...

  3. unity中全屏背景图缩放

    using UnityEngine; using System.Collections; public class BgPicScript : MonoBehaviour { // Use this ...

  4. input屏蔽历史记录

    设置input 的扩展属性 autocomplete为off即可 <input type="text" autocomplete="off" />

  5. css中选择器的使用

    css是英文Cascading Style Sheets的缩写.它是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言.我们再将html比喻 ...

  6. mm/kmalloc.c

    /* *  linux/mm/kmalloc.c * *  Copyright (C) 1991, 1992  Linus Torvalds & Roger Wolff. * *  Writt ...

  7. C++中的vector

    opencv中用到了很多vector  整理一下 vector容器是一个模板类,可以存放任何类型的对象(但必须是同一类对象).vector对象可以在运行时高效地添加元素,并且vector中元素是连续存 ...

  8. LeetCode() Ugly Number II 背下来!

    一个别人,非常牛逼的思路,膜拜了!orz!!!! vector <int> results (1,1); int i = 0, j = 0, k = 0; while (results.s ...

  9. PlayerLog.lua --玩家登录通告

    print(">>Script: Player Log Message.") --CLASS 职业 --战士 --圣骑士 --猎人 --盗贼 --牧师 --死亡骑士 - ...

  10. YOLO: Real-Time Object Detection 安装和测试

    1.下载darknet git clone https://github.com/pjreddie/darknet.git 2.修改make GPU= CUDNN= OPENCV= DEBUG= 3. ...