LeetCode之Easy篇 ——(1)Two Sum
1、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].
解法一:
暴力解决很简单,但时间复杂度为O(n^2)。
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result=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){
return new int[]{i,j};
}
}
}
return result;
}
}
解法二:
先遍历一遍数组,建立map数据,然后再遍历一遍,开始查找,找到则记录index。时间复杂度为O(n)。
public class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
int[] res = new int[2];
for (int i = 0; i < nums.length; ++i) {
m.put(nums[i], i);
}
for (int i = 0; i < nums.length; ++i) {
int t = target - nums[i];
if (m.containsKey(t) && m.get(t) != i) {
res[0] = i;
res[1] = m.get(t);
break;
}
}
return res;
}
}
LeetCode之Easy篇 ——(1)Two Sum的更多相关文章
- LeetCode之Easy篇 ——(7)Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...
- Leetcode解题思路总结(Easy篇)
终于刷完了leetcode的前250道题的easy篇.好吧,其实也就60多道题,但是其中的套路还是值得被记录的. 至于全部code,请移步github,题目大部分采用python3,小部分使用C,如有 ...
- leetcode面试准备:Minimum Size Subarray Sum
leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...
- [Leetcode 40]组合数和II Combination Sum II
[题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...
- [Leetcode 39]组合数的和Combination Sum
[题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...
- LeetCode Array Easy 167. Two Sum II - Input array is sorted
Description Given an array of integers that is already sorted in ascending order, find two numbers s ...
- LeetCode Array Easy 1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- c++ LeetCode (初级字符串篇) 九道算法例题代码详解(二)
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/11089327.html 已经刷了很多篇leetcode题了,不过最近在找c++的实习工作(大佬 ...
- leetcode@ [124] Binary Tree Maximum Path Sum (DFS)
https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...
随机推荐
- SpringMvc出现Jsp页面不能正常解析成html网页
<!-- Spring mvc配置 --> <servlet> <servlet-name>springMvc</servlet-name> <s ...
- HDU - 2187 贪心
思路: 按照单价升序排序,依次买就行. AC代码 #include <cstdio> #include <cmath> #include <algorithm> ...
- 内置函数 -- filter 和 map
参考地址:http://www.cnblogs.com/sesshoumaru/p/6000788.html 英文文档: filter(function, iterable) Construct an ...
- Typescript 基础应用
什么是 TypeScript TypeScript 是微软开发的 JavaScript 的超集,TypeScript兼容JavaScript,可以载入JavaScript代码然后运行.TypeScri ...
- 基于全志H3芯片的ARM开发环境搭建
基于全志H3芯片的ARM开发环境搭建 最近买了个友善之臂的NanoPi M1板子,又在网上申请了个NanoPi NEO板子,这两个都是基于全志H3芯片的Crotex-A7四核ARM开发板,两个板子可以 ...
- R+先知︱Facebook大规模时序预测『真』神器——Prophet(遍地代码图)
经统专业看到预测的packages都是很眼馋的.除了之前的forecast包,现在这个prophet功能也很强大.本packages是由机器之心报道之后,抽空在周末试玩几小时.一些基本介绍可见机器之心 ...
- 利用Caffe训练模型(solver、deploy、train_val)+python使用已训练模型
本文部分内容来源于CDA深度学习实战课堂,由唐宇迪老师授课 如果你企图用CPU来训练模型,那么你就疯了- 训练模型中,最耗时的因素是图像大小size,一般227*227用CPU来训练的话,训练1万次可 ...
- 【转载】Ubuntu Android开发环境搭配
Ubuntu Android开发环境搭配 安装Ubuntu Android应用程序开发环境需要如下几个软件 Java开发包:JDK 1.5/1.6 开发集成环境(IDE): Eclipse 3 ...
- xp+WinDBG+VMware调试内核
呵呵,搞点突兀的标题而已.其实说的还是如何使用WinDBG和VMware来搭建调试内核的环境而已,这些网上已经有数不清的教程了,不过我喜欢自己亲手写一下.第一,把这个过程写一遍能加深印象,就算以后忘记 ...
- Java获取当前的年月
今天,我在尝试从数据库取数据的过程中,发现页面初始化时需要给时间控件赋初值.于是,我就写了一个获取当前年月的时间工具类. 1.具体源码如下: YearAndMonth.java: /** * @Tit ...