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的更多相关文章

  1. 【LeetCode OJ】Sum Root to Leaf Numbers

    # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...

  2. 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 ...

  3. 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 ...

  4. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  5. [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 ...

  6. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  7. 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 ...

  8. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  9. [leetCode][013] Two Sum 2

    题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...

随机推荐

  1. sql_autoload_register()函数

    复习__autoload的时候,看到了spl_autoload_register()这个函数.但是一下子没有弄明白,通过查资料我算是弄明白了. 1.__autoload()    ——    自动加载 ...

  2. pycharm安装 suds模块报错:AttributeError: module 'pip' has no attribute 'main'

    需求:安装suds模块 遇到的问题: 一.报错信息:[file][Default Settint]---Project Interpreter 点击 搜索suds安装模块报错 解决:依据上图提示找到C ...

  3. 【jquery】 选中复选框 和 return false 的影响

    $('id').attr('checked',true); return false;   如果后面接上return false 的话,复选框的钩钩不会改变,但是.is(':checked')仍然能检 ...

  4. 有关nmap的5个常用的扫描指令

    [以下IP可替换成需要被测试的IP网段] 1.ping扫描:扫描192.168.0.0/24网段上有哪些主机是存活的: nmap -sP 192.168.0.0/24   2.端口扫描:扫描192.1 ...

  5. leetcode-12-stack

    409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...

  6. 纯虚函数(pure virtual function )和抽象类(abstract base class)

    函数体=0的虚函数称为“纯虚函数”.包含纯虚函数的类称为“抽象类” #include <string> class Animal // This Animal is an abstract ...

  7. Linux下ioctl函数理解

    一. 什么是ioctl ioctl是设备驱动程序中对设备的I/O通道进行管理的函数.所谓对I/O通道进行管理,就是对设备的一些特性进行控制,例如串口的传输波特率.马达的转速等等.它的调用个数如下: i ...

  8. JavaSE——Java对象导论

    一.抽象过程 人们所能够解决问题的复杂性直接取决于抽象的类型和质量.所谓抽象的类型指的是抽象的是什么,汇编语言是对底层机器的轻微抽象,命令式语言(FORTRAN.BASIC.C)是对汇编语言的抽象.这 ...

  9. Python 前端的第三方库

    sweetalert sweeralert:地址 这个使用很简单,需要在在他们的,css和js文件. 酷炫的结果    datatables datatables:地址 已https://datata ...

  10. 前端 五——ajax

    内容概要: 1.ajax的特点 2.基于JS的ajax 3.基于jQuery的ajax 1.特点: 局部刷新 异步传送(交互) 缺点: (1)无形中向服务器发送的请求次数太多,导致服务器压力增大. ( ...