LeetCode 之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.
Example:
Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].解题思路:考虑使用hashmap,将答案存于一个二维数组当中,遍历数组,将数组中的每个值与他对应的下标存储在hashmap中,直至找到一个数与前面的某一个数的和为target,找到这两个数对应的下标,存在二维数组当中。
public class Solution {public int[] twoSum(int[] nums, int target) {int []result = new int[2] ;Map<Integer, Integer> map= new HashMap<Integer, Integer>();for(int i=0;i<nums.length;i++){if(map.containsKey(target-nums[i])){result[1]=i;result[0]=map.get(target-nums[i]);return result;}map.put(nums[i], i);}return result;} }
转载于:https://www.cnblogs.com/mozhuyazhi/p/6535883.html
LeetCode 之Two Sum相关推荐
- leetcode 304. Range Sum Query 2D - Immutable |304. 二维区域和检索 - 矩阵不可变(二维前缀和问题)
题目 https://leetcode.com/problems/range-sum-query-2d-immutable/ 题解 本题是 medium 难度,二维前缀和问题.相似题目有: Easy: ...
- 【动态规划】LeetCode 377. Combination Sum IV
LeetCode 377. Combination Sum IV Solution1: 我的未能AC的答案 题目描述的和前几道题差不多,但实际上不能用DFS来做(会超时),要用动态规划,还是记录一下吧 ...
- 【DFS】LeetCode 39. Combination Sum
LeetCode 39. Combination Sum Solution1: DFS,这个套路要熟记啊! class Solution { public:vector<vector<in ...
- LeetCode 167.Two Sum II 解题报告
LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...
- leetcode 1005 Maximize Sum Of Array After K Negations
leetcode 1005 Maximize Sum Of Array After K Negations 1.题目描述 2.解题思路 3.Python代码 1.题目描述 给定一个整数数组 A,我们只 ...
- [勇者闯LeetCode] 1. Two Sum
[勇者闯LeetCode] 1. Two Sum Description Given an array of integers, return indices of the two numbers s ...
- [勇者闯LeetCode] 112. Path Sum
[勇者闯LeetCode] 112. Path Sum Description Given a binary tree and a sum, determine if the tree has a r ...
- 数据结构线段树介绍与笔试算法题-LeetCode 307. Range Sum Query - Mutable--Java解法
此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 线段树(Segme ...
- LeetCode 653. Two Sum IV - Input is a BST--Python解法
题目地址:Two Sum IV - Input is a BST - LeetCode Given a Binary Search Tree and a target number, return t ...
- LeetCode 167. Two Sum II - Input array is sorted--Python解法
题目地址:Two Sum II - Input array is sorted - LeetCode Given an array of integers that is already sorted ...
最新文章
- 经验 | 图像分割的实用技巧
- Facebook使用机器学习手段来自动优化其系统性能
- Linux (Ubuntu)使用vi和vim方向键变成了ABCD
- centos7中使用yum安装tomcat mysql 等
- IBM发布IBM Watson创新功能,旨在帮助企业扩展AI使用
- spark写mysql优化简书_spark——通过jdbc连接数据库中遇到的问题
- 数据结构与算法 | 顺序表
- Halcon算子学习:smooth_object_model_3d
- 基于SSM的实验室预约系统
- 【数字信号】基于matlab GUI数字电子琴【含Matlab源码 272期】
- 计算机信息与自然科学,郑旭飞 - 计算机与信息科学学院 - Powered by 西南大学
- ubuntu16.04.3下安装NVIDIAgtx1060显卡驱动记录
- win10 uwp 相机的分辨率设置方法
- linux禁用137 138端口,如何关闭135、137、138、139、445端口方法
- [LiteratureReview]A Collaborative Visual SLAM Framework for Service Robots
- MACBOOK 连接不上wifi的解决办法
- cinder云硬盘type创建
- Dremel和Hadoop
- vue3加载动态图片
- Python作为移动客户端后台服务器
热门文章
- xa协议 mysql_分布式事务XA实现数据一致性的协议与原理
- How to solve C library not available dtaidistance
- python怎么实现输入多行文字_介绍一个Python 包,几行代码可实现 OCR 文本识别!...
- mysql数据库dao模式_古诗MySQL数据库DAO模式实现
- Elasticsearch入门hello world (macos)【二】java请求Elasticsearch
- 编程实现 4 条重写规则,可生成半法式和法式
- 【项目管理】ITTO-整合管理
- myeclipse优化
- 也谈谈:《设计师如何掌握主动权》读后感
- IT 部门事件管理模式建立分析