LeetCode & Q1-Two Sum-Easy
Array Hash Table
Question
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].
我的解答如下:
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] temp = new int[2];
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
temp[0] = i;
temp[1] = j;
}
}
}
return temp;
}
}
时间复杂度:$O(n^{2})$
空间复杂度:$O(1)$
最优解:
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement) && map.get(complement) != i) {
return new int[] { i, map.get(complement) };
}
}
throw new IllegalArgumentException("No two sum solution");
}
时间复杂度为:$O(n)$
LeetCode & Q1-Two Sum-Easy的更多相关文章
- 【leetcode】Two Sum (easy)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [leetcode] #112 Path Sum (easy)
原题链接 题意: 给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值. 思路: DFS Runtime: 4 ms, faster than 100.00% of C++ class ...
- [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 ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- LeetCode算法题-Sum of Left Leaves(Java实现)
这是悦乐书的第217次更新,第230篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第85题(顺位题号是404).找到给定二叉树中所有左叶的总和.例如: 二叉树中有两个左叶 ...
- LeetCode算法题-Sum of Two Integers(Java实现)
这是悦乐书的第210次更新,第222篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第78题(顺位题号是371).计算两个整数a和b的总和,但不允许使用运算符+和 - .例 ...
- LeetCode--Array--Two sum (Easy)
1.Two sum (Easy)# Given an array of integers, return indices of the two numbers such that they add u ...
- 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算法积 ...
随机推荐
- 31.Django缓存和信号
缓存 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将某个views的返回值保存至内存或者memcache中, ...
- htop命令使用详解
一.htop 简介 htop 是Linux系统中的一个互动的进程查看器,一个文本模式的应用程序(在控制台或者X终端中),需要ncurses.与Linux传统的top相比,htop更加人性化.它可让用户 ...
- 自动化之路 Graphite监控上手指南
自动化运维怎能少了监控,推荐Graphite监控,下面是配置地址 http://www.infoq.com/cn/articles/graphite-intro/ Graphite官网 http:// ...
- 关于win8的各种版本的区别
Windows8.1 Professional VL 表示:专业版(大客户版,批量授权) Windows8.1 Multiple editions 表示:多合一版本(包含:标准版.专业版) 个人用户 ...
- TCP/IP NAT知识梳理
一. IP地址的获取 首先,互联网上的每台主机都有一个唯一的IP地址标识,计算机在通信时需要向网络中的DHCP(动态主机配置协议)服务器申请一个IP地址,但开始主机并不知道哪台机器是DHCP服务器(不 ...
- 共享MFC自绘Listctrl代码
在别人代码基础上修改的ListCtrl,支持设置行高,header高度,header背景图,奇偶行不同背景色, 支持设置某列为Checkbox,Edit,Combobox, 支持自定义排序. 效果图如 ...
- Java 面试宝典-2017
http://www.cnblogs.com/nelson-hu/p/7190163.html Java面试宝典-2017 Java面试宝典2017版 一. Java基础部分........... ...
- java项目和java-web项目中文件和文件夹的含义
1. java项目 .project:是工程构建配置文件 .classpath:保存的是项目所用的外部引用包的路径 .settings:记录项目配置变化的记录文件夹 src:sourcefolder项 ...
- curl/libcurl获取打开网页平均网速
CURL: curl -o /dev/null -s -w %{http_code}:%{http_connect}:%{content_type}:%{time_namelookup}:%{time ...
- 笔记:Maven 创建 Nexus 私服
首先从 http://nexus.sonatype.org/downloads/ 下载最新版本的Nexus,下载 bundle 包,不需要Web容器. windows 系统安装 目录结构说明 目录 说 ...