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 ...
随机推荐
- Eclipse使用Mybatis-Generator插件
Mybatis-Generator插件极大地方便了我们的开发效率,不用每张表每个字段人工去敲,所以本文介绍使用Mybatis-Generator自动生成Dao.Model.Mapping相关文件 版权 ...
- vue组件:canvas实现图片涂鸦功能
方案背景 需求 需要对图片进行标注,导出图片. 需要标注N多图片最后同时保存. 需要根据多边形区域数据(区域.颜色.名称)标注. 对应方案 用canvas实现涂鸦.圆形.矩形的绘制,最终生成图片bas ...
- Python3 pymsyql 连接读取数据
import pymysql conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='root', db='t ...
- leepcode作业解析-5-21
25.Nim游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编 ...
- python寻找模块的路径顺序
>>> import sys >>> sys.path ['', '/Library/Frameworks/Python.framework/Versions/3. ...
- LeetCode(143) Reorder List
题目 Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do ...
- 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)
A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...
- jenkins执行构建并查看结果
继完成构建项目配置http://www.cnblogs.com/yajing-zh/p/5111060.html后,则要执行构建. 回到jenkins主页之后,我们看到一个新建的项目显示出来: 点击进 ...
- JAVA-基础(一)
1.一个变量可以声明为final,这样做的目的是阻止它的内容被修改.这意味着在声明final 变量的时候,你必须初始化它(在这种用法上,final类似于C/C++中的const). 例如: final ...
- Oralce重做日志(Redo Log)
1.简介 Oracle引入重做日志的目的:数据库的恢复. Oracle相关进程:重做日志写进程(LGWR). 重做日志性质:联机日志文件,oracle服务器运行时需要管理它们. 相关数据字典:v$lo ...