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. BZOJ2132 圈地计划 【最小割】

    题目 最近房地产商GDOI(Group of Dumbbells Or Idiots)从NOI(Nuts Old Idiots)手中得到了一块开发土地.据了解, 这块土地是一块矩形的区域,可以纵横划分 ...

  2. caffe+Ubuntu14.04.10 +cuda7.0/7.5+CuDNNv4 安装

    特别说明: Caffe 官网地址:http://caffe.berkeleyvision.org/ 本文为作者亲自实验完成,但仅限用于学术交流使用,使用本指南造成的任何不良后果由使用者自行承担,与本文 ...

  3. 洛谷P1816 忠诚

    P1816 忠诚 569通过 1.5K提交 题目提供者该用户不存在 标签云端 难度普及+/提高 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 主席树的常数貌似大于线段树… TL ...

  4. (...)ES6三点扩展运算符

    扩展运算符将一个数组转为用逗号分隔的参数序列 console.log(...[a, b, c]) // a b c 用于: 1 将一个数组,变为参数序列 let add = (x, y) => ...

  5. 【08】Vue 之 vue-cli

    8.1. 前置知识学习 npm 学习 官方文档 推荐资料 npm入门 npm介绍 需要了解的知识点 package.json 文件相关配置选项 npm 本地安装.全局安装.本地开发安装等区别及相关命令 ...

  6. group by timestamp

    SELECT DATE_FORMAT( deteline, "%Y-%m-%d %H" ) , COUNT( * )  FROM test GROUP BY DATE_FORMAT ...

  7. windows 加入域

    点击computer,右击选system ,点 change setting,填写domain和computer-name 加入域,下次登陆加入,在域中会检查computer name

  8. 【MFC - 菜单】在对话框程序中添加菜单栏(CMenu)(转)

    原文转自 http://lishiqiang1988.blog.163.com/blog/static/41147912201382104631547/ VS2010的MFC对话框程序中添加菜单栏的过 ...

  9. 关于platform_device和platform_driver的匹配【转】

    转自:http://blog.csdn.net/dfysy/article/details/5959451 版权声明:本文为博主原创文章,未经博主允许不得转载. 说句老实话,我不太喜欢现在Linux ...

  10. Java EE学习记录(一)

    话说大家都在说java EE,但是java EE的分层结构如下: 1.数据持久层:主要由一些负责操作POJO(Plain Old Java Object)的类构成,主要负责将数据保存进入数据库; 2. ...