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的更多相关文章

  1. LeetCode之Easy篇 ——(7)Reverse Integer

    7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...

  2. Leetcode解题思路总结(Easy篇)

    终于刷完了leetcode的前250道题的easy篇.好吧,其实也就60多道题,但是其中的套路还是值得被记录的. 至于全部code,请移步github,题目大部分采用python3,小部分使用C,如有 ...

  3. leetcode面试准备:Minimum Size Subarray Sum

    leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...

  4. [Leetcode 40]组合数和II Combination Sum II

    [题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...

  5. [Leetcode 39]组合数的和Combination Sum

    [题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...

  6. 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 ...

  7. 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 ...

  8. c++ LeetCode (初级字符串篇) 九道算法例题代码详解(二)

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/11089327.html 已经刷了很多篇leetcode题了,不过最近在找c++的实习工作(大佬 ...

  9. 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 ...

随机推荐

  1. Yii的URL助手

    Url 帮助类 获得通用 URL 记住 URLs 检查相对 URLs Url 帮助类提供一系列的静态方法来帮助管理 URL. 获得通用 URL 有两种获取通用 URLS 的方法 :当前请求的 home ...

  2. 正"/" 和 反"\"的区别?

    反斜杠"\"是电脑出现了之后为了表示程序设计里的特殊含义才发明的专用标点.就是说,除了程序设计领域外,任何地方你都不应该有使用反斜杠的时候,请永远使用正斜杠"/" ...

  3. MysqL自动提交机制的关闭

    MysqL在执行一句数据库操作命令的时候,通常都是自动提交的.常用引擎下有两种,分别是MyIsam和InnoDB,MyIsam是不支持事务处理的,但InnoDB支持,但InnoDB在不开启事务处理的情 ...

  4. 生成1~n的排列

    直接递归打印. 代码如下 #include<cstdio> void dfs(int *a,int cur,int n) { if(cur==n) { for(int i=0;i<n ...

  5. Spring 框架系列之事务管理

    1.事务回顾 (1).什么是事务: 事务是逻辑上的一组操作,组成这组操作的各个逻辑单元,要么一起成功,要么一起失败. (2).事务特性(ACID) 原子性 :强调事务的不可分割 一致性 :事务的执行的 ...

  6. hbase存储优化

    1.上面的2张图主要说明hbase的存储特点 (1).每个值(每条记录的每一个列的值)的存储,都完整的存储了rowkey.column family.column.版本(时间戳),以及该列的值. 这样 ...

  7. websocket 和 ansible配合Tomcat实时日志给前端展示

    业务流程图如下 效果图展示 1.django安装websocket模块 pip install dwebsocket 2.shell脚本 用来传递不同的行号输出不同的内容; 第一个参数为0的时候,默认 ...

  8. SDP(9):MongoDB-Scala - data access and modeling

    MongoDB是一种文件型数据库,对数据格式没有硬性要求,所以可以实现灵活多变的数据存储和读取.MongoDB又是一种分布式数据库,与传统关系数据库不同的是,分布式数据库不支持table-join,所 ...

  9. 【其他】3dmax撤销Ctrl+z不能用的解决办法

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/3dmax_ctrlz.html 如果你经常去网上下载各种模型参考学习的话,出现这个问题的概率会非常高.因为出现这个 ...

  10. NLP+语义分析(四)︱中文语义分析研究现状(CIPS2016、角色标注、篇章分析)

    摘录自:CIPS2016 中文信息处理报告<第二章 语义分析研究进展. 现状及趋势>P14 CIPS2016> 中文信息处理报告下载链接:http://cips-upload.bj. ...