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 ...
随机推荐
- cookie与session的比较
首先来说一下什么是cookie:cookie是Web服务器保存在客户端的一系列文本信息: cookie的作用大致有三点:对特定对象的追踪,统计网页浏览次数,简化登陆. 它的安全性能是比较差的,容易泄露 ...
- java-JProfiler(三)-进行本地JVM的性能监控-监视本地java程序
1.打开JProfiler 默认会启动快速窗口[或者使用菜单Session→Start Center]打开 这里监视本地java程序,故在 主界面 2.查看监视界面 这时就可以查看 Instance ...
- 第k小数据
给定两个整型数组A和B(未排序).我们将A和B中的元素两两相加可以得到数组C. 譬如A为[1,2],B为[3,4].那么由A和B中的元素两两相加得到的数组C为[4,5,5,6]. 现在给你数组A和B, ...
- Input消除自动记忆功能
在html里就可以直接清除了<input type="text" autocomplete="off"> input 的autocomplete属性 ...
- CUDA显卡运算编程菜鸟入门指南1——Hello world - yfszzx的专栏 - 博客频道 - CSDN.NET
第一次知道有显卡(GPU)编程这个东西,是去年比特币最热门的时候,看了几篇关于比特币的文章,说比特币挖矿要靠显卡,CPU的速度与GPU根本就没法比,于是就非常好奇,显卡是什么神奇的东西?为什么运算速度 ...
- MFC Ribbon界面设计
Ribbon是类似于office2007样式的界面,它替代了传统的MFC程序里的菜单和工具栏 MFC默认生成的Ribbon功能少,需要我们自己添加一些控件和图片等元素使界面好看 看下面的一个界面,是V ...
- python中repr和eval可以用来在数据结构和字符串间互转
在这个功能上,repr和str的作用一样,把一个数据结构转换成字符串,例如: >>> str([1,2,3,4])'[1, 2, 3, 4]' >>> repr([ ...
- Secure CRT 自动记录日志log配置
SecureCRT8.0的下载地址下载地址: 链接:https://pan.baidu.com/s/1i5q09qH 密码:4pa2 配置自动log操作如下: 1.options ---> Se ...
- 【基于EF Core的Code First模式的DotNetCore快速开发框架】完成对DB First代码生成的支持
前言 距离上一篇文章<基于EF Core的Code First模式的DotNetCore快速开发框架>已过去大半个年头,时光荏苒,岁月如梭...比较尴尬的是,在这大半个年头里,除了日常带娃 ...
- 容器技术与DevOps
容器技术的使用支撑了目前 DevOps 三大主要实践:工作流.及时反馈.持续学习. 有人说容器技术与 DevOps 二者在发展的过程中是互相促进的关系.得益于 DevOps 设计理念的流行,容器生态系 ...