Leetcode Array 1 twoSum
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的更多相关文章
- leetcode ----ARRAY TWOSUM
代码的(判断nums[i]或者是target-nums[i]都可以):
- LeetCode初体验—twoSum
今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...
- [LeetCode] Array Nesting 数组嵌套
A zero-indexed array A consisting of N different integers is given. The array contains all integers ...
- [LeetCode] Array Partition I 数组分割之一
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...
- leetcode题解 1.TwoSum
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- 【LeetCode】001. TwoSum
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- Leetcode Array 4 Median of Two Sorted Arrays
做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法 ...
- 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 ...
- LeetCode Array Easy 88. Merge Sorted Array
Description Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted ar ...
随机推荐
- POJ3683 Priest John's Busiest Day 【2-sat】
题目 John is the only priest in his town. September 1st is the John's busiest day in a year because th ...
- 如何在vue里实现同步阻塞请求,请求完成之前不加载页面或组件?
https://segmentfault.com/q/1010000013787292 答案: getUserInfo函数数据请求下来,设置开关flag=true,页面最外层box v-if=&quo ...
- 【21】vuex 与element iu表单校验
转:http://www.cnblogs.com/gsgs/p/6753682.html element-ui的官网上写的自定义表单验证,方法都是写在单vue文件中的,不容易共享.怎么使用vuex将方 ...
- 使用 swagger组件给asp.net webapi文档生成
1.名词解释 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模 ...
- 百度云中有关IE浏览器的源码
<!--[if lt IE 9]> <div class="topbar">百度云控制台不支持当前所使用的浏览器,推荐安装 <a href=&qu ...
- [LeetCode] Trapping Rain Water 栈
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- Swift Perfect 服务器配置(Ubuntu16.0.4 主机、虚拟机)
Mac 开发环境 brew install mysql@5.7 && brew link mysql@5.7 --force mysql.server startmysql_secur ...
- python commands 模块
commands 模块 通过python调用系统命令 只适用于linux commands是提供linux系统环境下支持使用shell命令的一个模块 commands.getoutput(cmd) 只 ...
- map、hash_map、unordered_map 的思考
#include <map> map<string,int> dict; map是基于红黑树实现的,可以快速查找一个元素是否存在,是关系型容器,能够表达两个数据之间的映射关系. ...
- [Machine Learning with Python] My First Data Preprocessing Pipeline with Titanic Dataset
The Dataset was acquired from https://www.kaggle.com/c/titanic For data preprocessing, I firstly def ...