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. jquery 隐私迭代

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  2. Linux学习之“vfork函数”

    为什么使用vfork()? 希望父子进程执行不同的代码.例如: 网络服务程序中,父进程等待客户端的服务请求,当请求达到时,父进程调用fork,使子进程处理该次请求,而父进程继续等待下一个服务请求到达. ...

  3. WPF 鼠标在图片Image上悬停时切换更改设置图片源Source

    // 无效的写法,图片不会被切换 <Image Margin="0,0,0,0" Width="50" Height="50" Sou ...

  4. VS.NET版本与VC版本对应关系

    VC++编译器版本 IDE版本 VC6 VC6.0 VC7 VS2003 VC8 VS2005 VC9 VS2008 VC10 VS2010 VC11 VS2012 VC12 VS2013 VC14 ...

  5. NAudio的使用说明

    官方网站:http://naudio.codeplex.com/ 源码:https://github.com/naudio/NAudio NuGet安装: Install-Package NAudio ...

  6. BackgroundWorker 简单使用教程 多个线程的创建

    原文:BackgroundWorker 简单使用教程 多个线程的创建 BackgroundWorker是一个非常不错的线程控件,能避免界面假死,让线程操作你想要做的事,它学习起来很简单,但是能实现很强 ...

  7. Android零基础入门第62节:搜索框组件SearchView

    原文:Android零基础入门第62节:搜索框组件SearchView 一.SearchView概述 SearchView是搜索框组件,它可以让用户在文本框内输入文字,并允许通过监听器监控用户输入,当 ...

  8. UWP开发-获取设备唯一ID

    EasClientDeviceInformation deviceInfo = new EasClientDeviceInformation(); this.showDeviceInfo.Items. ...

  9. Change Default Route

    route delete 0.0.0.0route add 0.0.0.0 mask 0.0.0.0 10.226.4.14

  10. Model1简介

    Model1模型出现前,整个Web应用的情况:几乎全部由JSP页面组成,JSP页面接收处理客户端请求,对请求处理后直接做出响应. 弊端:在界面层充斥着大量的业务逻辑的代码和数据访问层的代码,Web程序 ...