1_Two Sum
1.Two Sum
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].
记录第一个LeetCode题, c++的STL库不会用, (╥╯^╰╥)
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> ret;
for (int i = 0; i < nums.size() - 1; i++) {
for (int j = i + 1; j < nums.size(); j++) {
if(target == (nums[i] + nums[j])){
ret.push_back(i);
ret.push_back(j);
return ret;
}
}
}
return ret;
}
};
1_Two Sum的更多相关文章
- 1_Two Sum --LeetCode
原题如下: 思路:将nums放到一个map<int,int>中,其中,键是nums中元素,值对应其下标.然后遍历nums,取nums中一个值nums[i],接着用target减去它,最后再 ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- 06 解决Sublime Text3输入法不跟随的问题
安装原生的Sublime, 输入法是不会跟随Sublime的编译文件页面的,会失去焦点,这样写代码写文档时看起来会十分不方便,参考了一些资料,下载插件做了配置,已经在自己机器上用百度输入法测试成功,记 ...
- 【题解】Ehab the Xorcist
\(\color{red}{Link}\) \(\color{blue}{\text{Solution:}}\) 题目要求构造一个最短的序列,使得异或和为\(u\),数列和为\(v\). 那么,因为是 ...
- NOI 2011 【阿狸的打字机】
之前讲了[AC自动姬],今天我终于把这题给刚下来了...嗯,来给大家讲一讲. 题目描述: 打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工 ...
- 【转载】动态规划—各种 DP 优化
原博客地址 关于氵博客:其实主要是防止我找不到这篇文了
- ansible使用playbook的简单例子(ansible2.9.7)
一,ansible使用playbook的优点 1,用ansible执行一些简单的任务,使用ad-hoc命令就可以解决问题 如果执行复杂的功能,需要大量的操作,执行的ad-hoc命令会不够方便,这时我们 ...
- Ubuntu18.04中安装virtualenv和virtualenvwrapper
1.安装virtualenv和virtualenvwrapper pip3 install virtualenv pip3 install virtualenvwrapper 2.创建目录用来存放虚拟 ...
- js获取页面高度
<script> function getInfo() { var s = ""; s += " 网页可见区域宽:"+ document.body. ...
- 浏览器页面左上角出现undefined
浏览器页面左上角出现undefined, js文档中: let list; list += html代码; 解决办法: let list = html代码;
- 分布式机器学习:如何快速从Python栈过渡到Scala栈
首先介绍下我的情况和需求,如果你跟我类似,那么这篇文章将很有帮助: 我之前的技术栈主要是Java.Python,机器学习方面主要用到是pandas.numpy.sklearn.scipy.matplo ...
- ServletContext使用介绍
ServletContext是一个容器(域对象)可以存储键值对数据(String key,Object value),保存在ServletContext中的数据不仅可以提供给所有的servlet使用, ...