Leetcode[1]Two Sum C++
最简单的思想,遍历,
1、两层循环,自己写的,没有用STL,时间花费较长
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
for(int i=;i<nums.size();i++){
int temp = target-nums[i];
for(int j=i+;j<nums.size();j++){
if(temp==nums[j]){
result.push_back(i);
result.push_back(j);
return result;
}
}
}
return {, };
}
2、使用Map先插入元素,再对map内的元素进行搜索
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map;
for (int i = ; i < nums.size(); i ++) {
auto it = map.find(target - nums[i]);
if (it != map.end()) {
return { i, it->second };
}
map[nums[i]] = i;
}
return { , };
}
Leetcode[1]Two Sum C++的更多相关文章
- 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 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
随机推荐
- docker的安装,使用
1:安装:https://blog.csdn.net/yx_222/article/details/80936757 sudo apt-get install docker.io 2: 使用dock ...
- ppt插入声音
1:点击插入>音频>文件中的音频 2:插入成功后,会出现一个声音的图表 3:对播放格式进行设置,设置循环播放等. 4:双击对声音进行编辑 ,会出现右边的各个组件, 5:点击下拉框>效 ...
- SVN出现xcrun: error: invalid active developer path(Mac)
Mac升级了系统,配置PHPStorm的SVN,出现如下错误: 具体提示的内容是:xcrun: error: invalid active developer path (/Library/Devel ...
- python3与mysql:创建表、插入数据54
import pymysql db = pymysql.connect(host=',db='jodb1',port=3307,charset='utf8') # #测试连接开发库成功 # db = ...
- Java jdbc 操作数据库详解
原文地址https://www.cnblogs.com/huguodong/p/5910859.html JDBC(Java Data Base Connectivity,java数据库连接)是一种用 ...
- js实现轮播图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C# 多线程,new ThreadStart(方法)中的方法如果有参数,该怎么写
using System; using System.Threading; public class Work { public static void Main() { // Start a thr ...
- excel输入数字变成特殊符号问题
问题,在单元格里输入数字,结果变成文件夹类型的小图片或特殊符号了,原因是字体为Wingdings,将其设为Times New Roman即可
- 集合框架—HashMap
HashMap提供了三个构造函数: HashMap():构造一个具有默认初始容量 (16) 和默认加载因子 (0.75) 的空 HashMap. HashMap(int ini ...
- OpenStack trove原理及配置实践
DBaaS是什么? 字面上理解数据库即是服务,简单来说就是以服务的形式为用户提供数据库服务. 在云平台上使用trove有什么优势? 简化IT操作流程,降低使用数据库使用门槛举个例子,曾经我搭建一个LA ...