lleetcode 1 two sum c++
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++的更多相关文章
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [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 ...
随机推荐
- jquery 用json设置css
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- C# WindowsPrincipal(Windows规则)的使用
using System;using System.Collections.Generic;using System.Linq;using System.Security.Principal;usin ...
- laravel 增删修改
1.查 public function userTest(){ return $this->all(); } public function userTest(){ return $this-& ...
- VS生成Cordova for Android应用之Gradle
原文:VS生成Cordova for Android应用之Gradle 一.Gradle简介 Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化建构工具.它使用一种基 ...
- wpf事件绑定,比如一个控件的左键按下事件
<i:Interaction.Triggers> <i:EventTrigger EventName="MouseLeftButtonDown"> < ...
- vs2017 cordova调试android app
方案是:virtualbox + androidx86 7.0+_x64.iso虚拟机方式
- ARTS 12.10 - 12.14
从陈皓博主的专栏里学到一个概念,争取可以坚持下去: 我在我的读者群中推荐出 ARTS 的任务,每个人每周一个 Algorithm,Review 一篇英文文章,总结一个工作中的技术 Tip,以及 Sha ...
- Tcptrack —— TCP 连接的嗅探器
分享 <关于我> 分享 [中文纪录片]互联网时代 http://pan.baidu.com/s/1qWkJfcS 分享 <HTML开发MacOSAp ...
- Compile for Windows on Linux(交叉编译,在Linux下编译Windows程序),以OpenSSL为例
OpenSSL for Windows In earlier articles, we have looked at how to create a gcc build environment on ...
- 修改Maven的本地仓库地址
已经配置好的设定文件: 1.创建一个本地仓库的地址 2.修改Maven中conf目录下的settings.xml文件 在此处添加修改后的本地仓库的地址 3.打开cmd 输入mvn help:sys ...