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 ...
随机推荐
- vue里的数据
背景: 一个项目完工在即,鉴于此,前端使用了vue,写下此栏,以供日后翻阅, 会涉及到我所运用到的vue相关知识,需要一定的js基础. 默认vue的single-file-components(单文件 ...
- linux防火墙firewall使用简介
1.firewalld的基本使用启动: systemctl start firewalld查看状态: systemctl status firewalld停止: systemctl disable f ...
- poj 2376 选择工作区间问题 贪心算法
题意:给一些工作区间,如何选取最小的工作数量,覆盖[1,T]的工作时长 一开始的思路,当然也是错误的思路: 我想着,最小工作数量是吧?那肯定就是选择结束时间最晚的,给结束时间来一个排序.哎这个思路错误 ...
- 光学字符识别OCR-6 光学识别
经过前面的文字定位和文本切割,我们已经能够找出图像中单个文字的区域,接下来可以建立相应的模型对单字进行识别. 模型选择 在模型方面,我们选择了深度学习中的卷积神经网络模型,通过多层卷积 ...
- luogu2762 太空飞行计划问题
最大权闭合子图 参考这,胡伯涛论文. 10,8,6,3这个简单割对应的闭合子图是A1,B1,B2 输出路径时,最后一次层次图中,与源点相连的点即选做的实验,与汇点相连的点即选用的仪器. #includ ...
- 豆邮windows客户端(第三方)开发详解
“豆邮”,是社区网站“豆瓣”的一个类似私信的功能模块.在豆瓣官网,“豆邮”曾一度被改为“私信”,但在遭到众多豆瓣用户的强烈反对之后又改了回来.然而,在豆瓣的移动客户端上,仍称呼为“私信”. 豆邮的设定 ...
- Java EE - Servlet 3.0 和 Spring MVC
Table of Contents 前言 基于 Java 的配置 ServletContainerInitializer 动态配置 DispatcherServlet 和 ContextLoaderL ...
- js的编码函数
js对文字进行编码,涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent ...
- Git只获取部分目录的内容
Git只获取部分目录的内容 Git的克隆,默认是直接拉取整个远程仓库,如果项目比较大,大量和自己无关的内容也会拉到本地,占用很多硬盘空间.Git在1.7版本后,已经支持只Checkout部分内容,这个 ...
- oracle查询包含大小写的数据
查询包含小写的所有数据: select oper_no from info_oper where regexp_like(oper_no,'[[:lower:]]'); select oper_no ...