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 ...
随机推荐
- hash 哈希查找复杂度为什么这么低?
hash 哈希查找复杂度为什么这么低? (2017-06-23 21:20:36) 转载▼ 分类: c from: 作者:jillzhang 出处:http://jillzhang.cnblogs ...
- Unity基础-图形渲染
图形渲染-Camera Camera下的Clear Flags:Skybox,Don't Clear,Depth only(深度),Solid Color(固定颜色) Culling Mask:渲染层 ...
- Python使用gevent实现协程
Python中多任务的实现可以使用进程和线程,也可以使用协程. 一.协程介绍 协程,又称微线程.英文名Coroutine.协程是Python语言中所特有的,在其他语言中没有. 协程是python中另外 ...
- 大意了,这几道Python面试题没有答对,Python面试题No13
第1题: Python如何爬取 HTTPS 网站? 这类问题属于简单类问题 在使用 requests 前加入:requests.packages.urllib3.disable_warnings(). ...
- POJ:2411-Mondriaan's Dream(矩形拼接方案)
题目链接:http://poj.org/problem?id=2411 解题心得: 可以说是很经典的一个状压dp了,写dfs遍历肯定是要超时的,这个题的状态转移方程对新手来说有点吃力. 状态转移用的是 ...
- 笔记-python-*号解包
笔记-python-*号解包 在码代码时发现*号可以这样使用: str = ["abcd", "abce", "abcf"]st = &qu ...
- 有限状态机(FSM)的设计与实现
有限状态机(FSM)是表示有限个状态及在这些状态之间的转移和动作等行为的数学模型,在计算机领域有着广泛的应用.通常FSM包含几个要素:状态的管理.状态的监控.状态的触发.状态触发后引发的动作.本文主要 ...
- iframe内容刷新
经常有嵌套的iframe的内容无法及时刷新,需要手动刷新,这时候就需要获取iframe,然后调用对象的reload, document.getElementById(iframe的id).conten ...
- Freemaker模板指令
${...}:Freemaker将会输出真实的值来替换大括号内的表达式.这样的表达式被称为interpolation(插值). FTL标签(Freemaker模板的语言标签):FTL标签和HTML标签 ...
- tensorflow笔记
1.Estimator 进行编程的概览 要根据预创建的 Estimator 编写 TensorFlow 程序,您必须执行下列任务: 创建一个或多个输入函数. 定义模型的特征列. 实例化 Estimat ...