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]. C++:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> ans;
for(int i = 0;i < nums.size()-1;i++){
for(int j = i+1;j<nums.size();j++){
if(nums[i]+nums[j] == target){
ans.push_back(i);
ans.push_back(j);
break;
}
}
}
return ans;
}
};

Java:

 public class Solution {
public int[] twoSum(int[] nums, int target) {
int []a = new int[2];
for(int i =0;i<nums.length-1;i++){
for(int j=i+1;j<=nums.length-1;j++){
if(nums[i]+nums[j] == target){
a[0] = i;
a[1] = j;
break;
}
}
}
return a;
}
}

附加一下c++ vector 的简单用法:

1.push_back   在数组的最后添加一个数据
2.pop_back    去掉数组的最后一个数据 
3.at                得到编号位置的数据
4.begin           得到数组头的指针
5.end             得到数组的最后一个单元+1的指针
6.front        得到数组头的引用
7.back            得到数组的最后一个单元的引用
8.max_size     得到vector最大可以是多大
9.capacity       当前vector分配的大小
10.size           当前使用数据的大小
11.resize         改变当前使用数据的大小,如果它比当前使用的大,者填充默认值
12.reserve      改变当前vecotr所分配空间的大小
13.erase         删除指针指向的数据项
14.clear          清空当前的vector
15.rbegin        将vector反转后的开始指针返回(其实就是原来的end-1)
16.rend          将vector反转构的结束指针返回(其实就是原来的begin-1)
17.empty        判断vector是否为空
18.swap         与另一个vector交换数据

 
 

Leetcode Array 1 twoSum的更多相关文章

  1. leetcode ----ARRAY TWOSUM

    代码的(判断nums[i]或者是target-nums[i]都可以):

  2. LeetCode初体验—twoSum

    今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...

  3. [LeetCode] Array Nesting 数组嵌套

    A zero-indexed array A consisting of N different integers is given. The array contains all integers ...

  4. [LeetCode] Array Partition I 数组分割之一

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  5. leetcode题解 1.TwoSum

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

  6. 【LeetCode】001. TwoSum

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  7. Leetcode Array 4 Median of Two Sorted Arrays

    做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法 ...

  8. LeetCode Array Easy 167. Two Sum II - Input array is sorted

    Description Given an array of integers that is already sorted in ascending order, find two numbers s ...

  9. LeetCode Array Easy 88. Merge Sorted Array

    Description Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted ar ...

随机推荐

  1. FZU 2168 前缀和+dp递推

    Description   部队中共有N个士兵,每个士兵有各自的能力指数Xi,在一次演练中,指挥部确定了M个需要防守的地点,按重要程度从低到高排序,依次以数字1到M标注每个地点的重要程度,指挥部将选择 ...

  2. [01] radio ,checkbox 表单文字对齐

    http://www.cnblogs.com/wangsir015/p/5555818.html 这几天在做表单时总会碰到复选框(checkbox)和单选框(radio)与文字不对齐的问题,要不是ch ...

  3. HDOJ 1398 Square Coins

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  4. 利用$.getJSON() 跨域请求操作

    原文发布时间为:2011-01-14 -- 来源于本人的百度文章 [由搬家工具导入] $.get 没有权限? $.post 没有权限? 因为他们都不能跨域,那就用 $.getJSON() 吧 利用$. ...

  5. hdu 3518 Boring counting 后缀数组 height分组

    题目链接 题意 对于给定的字符串,求有多少个 不重叠的子串 出现次数 \(\geq 2\). 思路 枚举子串长度 \(len\),以此作为分界值来对 \(height\) 值进行划分. 显然,对于每一 ...

  6. onCreate、onStart、onResume、onPause、onStop、onDestory(转)

    程序正常启动:onCreate()->onStart()->onResume();正常退出:onPause()->onStop()->onDestory() 一个Activit ...

  7. 【原创】SSAS 实例重命名

    在某些时候我们可能想对现有的SSAS实例进行重命名之类的,比如:我以前有两个SSAS,一个2005,一个2008R2,其中我们2005是一开始安装的,并且是默认实例,2008R2是命名实例,但是随着使 ...

  8. react-transition-group v2 用法概述

    官方文档地址:https://reactcommunity.org/react-transition-group/ 在react-transition-group v2中,暴露了三个组件: Trans ...

  9. Java---杨辉三角简易解法(通俗易懂,逻辑严密)

  10. 《Microsoft SQL Server 2008 Internals》读书笔记--目录索引

    http://blog.csdn.net/downmoon/article/details/5256548 https://sqlserverinternals.com/companion/