LeetCode OJ——Two Sum
http://oj.leetcode.com/problems/two-sum/
求是否存在两个数的和为target,暴力法,两层循环
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; class Solution { public:
vector<int> twoSum(vector<int> &numbers, int target) {
// Note: The Solution object is instantiated only once and is reused by each test case.
//sort(numbers.begin(),numbers.end()); vector<int> answer;
answer.clear();
if(numbers.size()<)
return answer;
for(int index = ;index<numbers.size()-;index++)
{
for(int index2 = index+;index2<numbers.size();index2++)
{
if(numbers[index2] == target - numbers[index])
{
answer.push_back(index+);
answer.push_back(index2+);
return answer;
}
}
}
return answer;
}
}; int main()
{
Solution * mysolution = new Solution();
vector<int> numbers;
numbers.push_back();
numbers.push_back();
numbers.push_back();
numbers.push_back();
numbers.push_back();
numbers.push_back();
numbers.push_back();
vector<int> ans;
int i = ;
ans= mysolution->twoSum(numbers, );
while(i!=ans.size())
{
cout<<ans[i]<<" ";
i++;
}
return ;
}
LeetCode OJ——Two Sum的更多相关文章
- 【LeetCode OJ】Sum Root to Leaf Numbers
# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...
- LeetCode OJ 129. Sum Root to Leaf Numbers
题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...
- LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- [LeetCode] 303. Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
随机推荐
- Python9-hashilib模块-day28(大年初三)
__getitem__\__setitem__\__delitem__ class Foo: def __init__(self,name,age,sex): self.name = name sel ...
- istio的原理和功能介绍
目录 1 什么是Istio 2 架构和原理 2.1 Proxy代理 2.2 Mixer混合器 2.3 Pilot引导 2.4 Citadel堡垒 2.5 Galley 3 功能列表 4 性能评估 1 ...
- multiprocessing join与lock区别
加锁 join方法 join方法会造成阻塞,在上一个进程完成之前不会运行join()后面的代码 lock 仍会执行之后的代码,遇到创建进程,会发向操作系统发出指令,但不会执行,等到上锁的进程结束之后 ...
- Django Model two
Django_model: eg: class XXXX(models.Model): nid = models.AutoField(primary_Key=True) name = models.C ...
- webdriver高级应用- 禁止IE的保护模式
#encoding=utf-8 from selenium import webdriver from selenium.webdriver.common.desired_capabilities i ...
- python-高级编程-06-长连接&连接池
我们都知道tcp是基于连接的协议,其实这个连接只是一个逻辑上面的概念,在ip层来看,tcp和udp仅仅是内容上稍有差别而已. tcp 的连接仅仅是连接两端对于四元组和sequence号的一种约定而已 ...
- python redis中blpop和lpop的区别
python redis 中blpop返回的是元组对象,因此返回的时候注意 lpop返回的是对象
- Android library projects cannot be launched解决方法
着了一个例子项目,总是报标题说的错误. 解决方法如下: 红圈的地方,勾掉. 貌似如果你这个项目是作为一个被引用的project的话, 要勾上这个.单独作为一个app的话,不能勾选这个. --不懂,瞎写 ...
- linux dd命令创建一定大小的文件
http://www.cnblogs.com/jikexianfeng/p/6103500.html
- April Fools Contest 2018
这个比赛不正经,但是我可以一本正经的写代码啊 A. Quirky Quantifiers time limit per test 2 seconds memory limit per test 64 ...