LeetCode 1:Two Sum
用 Hash Table 实现
Time : O(n)
Space: O(n)
import java.util.HashMap;public class Solution {public int[] twoSum(int[] nums, int target) {HashMap< Integer, Integer> map = new HashMap<>();for (int i = 0; i < nums.length; i++) {if (map.containsKey(target-nums[i])) {int[] result = {map.get(target-nums[i])+1, i+1};return result;}map.put(nums[i], i);}return null;}public static void main(String[] args) {Solution solu = new Solution();int[] a = {2, 7, 11, 15};int target = 9;int[] result = solu.twoSum(a, target);for (int i = 0; i < result.length; i++)System.out.println(result[i]);} }
2015-10-13
转载于:https://www.cnblogs.com/whuyt/p/4875503.html
LeetCode 1:Two Sum相关推荐
- Leetcode题目:Range Sum Query - Immutable
题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
- LeetCode OJ:Combination Sum III(组合之和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 1. Two Sum [Easy] 题目: Given an array of integers, return indices of the two ...
- LeetCode实战:二叉树中的最大路径和
背景 为什么你要加入一个技术团队? 如何加入 LSGO 软件技术团队? 我是如何组织"算法刻意练习活动"的? 为什么要求团队的学生们写技术Blog 题目英文 Given a non ...
- LeetCode实战:最大子序和
题目英文 Given an integer array nums, find the contiguous subarray (containing at least one number) whic ...
- LeetCode实战:字符串相加
题目英文 Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and ...
- LeetCode实战:最接近的三数之和
题目英文 Given an array nums of n integers and an integer target, find three integers in nums such that ...
- LeetCode实战:三数之和
题目英文 Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? ...
- LeetCode实战:快乐数
题目英文 Write an algorithm to determine if a number is "happy". A happy number is a number de ...
最新文章
- Delphi XE5 常见问题解答
- 图像检索:几种基于纹理特征的图像检索算法
- Alpha(5/10)
- wave文件 fft_STM32F103 如何实现 FFT?
- Android开发之设置listview分割线的颜色
- java 两个线程同步_Java 多线程(二)—— 线程的同步
- iKcamp团队制作|基于Koa2搭建Node.js实战(含视频)☞ 中间件用法
- php cms 那个安全,PHPCMS系统安全设置步骤
- 北航计算机组成原理课程设计-2020秋 PreProject-Logisim-Logisim仿真与调试应用与挑战
- 下载安装MinGW-w64详细步骤(c/c++的编译器gcc的windows版,win10真实可用)
- l05173芯片针脚图_芯片引脚图及引脚描述
- 十大python开发软件-介绍6款实用的python编程开发工具
- MySQL生成36位、32位UUID以及32位大写的UUID
- description The server encountered an internal error that prevented it from fulfilling this request错
- ubuntu换源 树莓派
- 目标检测与位姿估计(五):A Survey: Visual Place Recognition
- SAP WM上架策略为Fixed Bin的Storage Type如何以FIFO出库?
- 《寄居者》 - 严歌苓
- BMP文件RGB颜色数据存放方式
- IE浏览器下实现rtmp、rtsp协议视频播放