Problem describe:https://leetcode.com/problems/two-sum/

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1]

AC code :

1.Bruth Algorithm

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> res;
for(int i = ;i < nums.size();i++)
for(int j = i + ;j < nums.size();j++)
if(nums[i]+nums[j]==target)
{
res.push_back(i);
res.push_back(j);
}
return res; }
};

2.Hashmap

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> res;
unordered_map<int,int> map;
for(int i = ; i < nums.size();i++)
{
map[nums[i]] = i;
}
for(int i = ;i < nums.size();i++)
{
int left = target - nums[i];
if(map.count(left) && i < map[left])
{
res.push_back(i);
res.push_back(map[left]);
}
}
return res;
}
};

lleetcode 1 two sum c++的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  2. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

随机推荐

  1. C/C++使用openssl进行摘要和加密解密(md5, sha256, des, rsa)

    openssl里面有很多用于摘要哈希.加密解密的算法,方便集成于工程项目,被广泛应用于网络报文中的安全传输和认证.下面以md5,sha256,des,rsa几个典型的api简单使用作为例子. 算法介绍 ...

  2. 每日一题:Java异常处理

    什么是异常 在理想情况下,程序总会运行在很完美的环境中,网络不会终端,文件一定存在,程序不会有 BUG.但是,理想很丰满,现实很骨干,实际生产环境中,网络可能会中断,文件可能会找不到,内存可能会溢出, ...

  3. Selenium 获取动态js的网页

    Selenium基于webkit实现爬虫功能 http://www.cnblogs.com/luxiaojun/p/6144748.html https://www.cnblogs.com/chenq ...

  4. Android零基础入门第27节:正确使用padding和margin

    原文:Android零基础入门第27节:正确使用padding和margin 前面两期我们学习了LinearLayout线性布局的方向.填充模型.权重和对齐,那么本期我们来学习LinearLayout ...

  5. 什么是OTC?

    OTC(Over The Counter)非处方药物,我国卫生部医政司是这样定义的:它是消费者可不经过医生处方,直接从药房或药店购买的药品,而且是不在医疗专业人员指导下就能安全使用的药品,即不需要凭借 ...

  6. Android零基础入门第46节:下拉框Spinner

    原文:Android零基础入门第46节:下拉框Spinner 上一期学习了GridView的使用,你已经掌握了吗?本期一起来学习Spinner的使用. 一.认识Spinner Spinner其实就是一 ...

  7. Qt-vs-addin失效的问题

    Qt-vs-addin的小问题 使用Visual Studio进行Qt开发的时候,需要安装一个插件.然而有时候这个插件的一些工具却莫名其妙的失效: 其中qt5appwrapper.exe用于编辑Qt工 ...

  8. Web 高效开发必备的 PHP 框架

    摘要: 每周为您推送最有价值的开源技术内参! 码云项目推荐 1.项目名称:多功能 THinkPHP 开源框架 项目简介:使用 THinkPHP 开发项目的过程中把一些常用的功能或者第三方 sdk 整合 ...

  9. 任何一件事,如果你不投入时间和精力去驯养,就不可能产生真正的兴趣和热爱(Focus Feedback FixIt的原理) good

    这两本书和我们说的兴趣结合起来,为我们指明了精进的道路: 选择一个你感兴趣的方向 刻意练习 持续投入时间和精力 所谓刻意练习,简单说就是“3F”,即: Focus Feedback Fix it Fo ...

  10. 朱晔的互联网架构实践心得S2E6:浅谈高并发架构设计的16招

    朱晔的互联网架构实践心得S2E6:浅谈高并发架构设计的16招 概览 标题中的高并发架构设计是指设计一套比较合适的架构来应对请求.并发量很大的系统,使系统的稳定性.响应时间符合预期并且能在极端的情况下自 ...