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 克隆div 复制div 克隆元素 复制元素
代码: $('.div1').clone() 定义和用法 clone() 方法生成被选元素的副本,包含子节点.文本和属性. 语法 $(selector).clone(includeEvents) 参数 ...
- C++和QML混合的QT程序调试方法
以前调试只是QML或者只是C++的QT程序很简单,断点打上,直接debug按钮一点,喝一口水,自然就停在断点了. 这次遇到C++和QML混合的程序,把CONFIG+=declarative_debug ...
- Android零基础入门第24节:自定义View简单使用
原文:Android零基础入门第24节:自定义View简单使用 当我们开发中遇到Android原生的组件无法满足需求时,这时候就应该自定义View来满足这些特殊的组件需求. 一.概述 很多初入Andr ...
- 16.Oct Working Note
01 writing algorithm by assembly,but the bug... now,it runs normaly,but how to print the answer? suc ...
- mysqldump数据库备份与恢复
mysqldump -u 用户名 -p 数据库名> 备份的文件名 本文中因服务器为多实例,所以在执行登陆等命令时指定了-S参数,即指定其中一个数据库 备份: mysqldump -u root ...
- Qt-vs-addin失效的问题
Qt-vs-addin的小问题 使用Visual Studio进行Qt开发的时候,需要安装一个插件.然而有时候这个插件的一些工具却莫名其妙的失效: 其中qt5appwrapper.exe用于编辑Qt工 ...
- QTcpSocket 对连接服务器中断的不同情况进行判定
http://blog.csdn.net/goforwardtostep/article/details/52300335
- Java基础(四) StringBuffer、StringBuilder原理浅析
StringBuilder与StringBuffer作用就是用来处理字符串,但String类本身也具备很多方法可以用来处理字符串,那么为什么还要引入这两个类呢? 关于String的讲解请看Java基础 ...
- JAVA8之lambda表达式详解
原文:http://blog.csdn.net/jinzhencs/article/details/50748202 lambda表达式详解 一.问题 1.什么是lambda表达式? 2.lambda ...
- 10 关于DOM的操作
一.JavaScript的组成 JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for语句等. DOM:文档对象 ...