leetcode 刷题记录
来源:互联网 发布:vs for mac 离线 编辑:程序博客网 时间:2024/05/19 22:44
尚未佩妥剑,转眼便江湖,记录一下自己写过的题
1.array
(1)twoSum
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.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
代码
遍历数组,将遍历过的vector存到map里面,键值为order,再去find,返回order。
using namespace std;#include <vector>#include <map>#include <iostream>class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { map <int,int> hashmap; vector <int> result; for (int i = 0; i < nums.size(); i++) { int toFind = target - nums[i]; if (hashmap.find(toFind) != hashmap.end()) { result.push_back(hashmap[toFind]); result.push_back(i+1); return result; } hashmap[nums[i]] = i; } }};//如果没有找到,返回null, vector的size为0,亲测。时间复杂度为o[n]
阅读全文
0 0
- leetcode刷题记录
- leetcode刷题记录
- leetcode刷题记录
- leetcode 刷题记录
- leetcode刷题记录
- Leetcode刷题记录
- Leetcode刷题记录
- leetcode 刷题记录
- LeetCode刷题记录
- 【LeetCode刷题记录】
- leetCode 刷题记录 Candy
- LeetCode 刷题记录 目录
- leetcode刷题记录4--依然数组
- leetcode刷题记录5--依旧数组
- [LeetCode刷题记录]Single Number II
- [LeetCode刷题记录]Partition List
- [LeetCode刷题记录]Number of Islands
- 【LeetCode刷题记录】Rotate Array
- tmp目录下文件被删除
- java 汇聚 拆分新亮点,试试新方法吧
- PostgreSQL 逻辑订阅
- xshell 脚本网站域名监控-邮件提醒
- django 模板日期格式
- leetcode 刷题记录
- rpm包进行升级和回退操作
- Google巨献
- 自定义控件id无法被识别的解决方法
- 完整的HTTP请求会经历以下过程
- LeetCode--Remove Nth Node From End of List(移除链表的倒数第N个节点)Python
- Codeforces Round #449 (Div. 2) 897C. Nephren gives a riddle
- springMVC Model、 ModelMap 和 ModelAndView的区别
- SpringAOP源码解析