LeetCode : two sum

第一次写博客,算是熟悉这些编辑环境吧,本来是打算在csdn上用markdown写的,结果改了博客介绍就被关闭了,晕死。。。好了,话不多说,今天打算拿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.

例子:Given nums = [2, 7, 11, 15], target = 9,

   Because nums[0] + nums[1] = 2 + 7 = 9,

     return [0, 1].


分析:看到题目之后了解到需求为找到数组中两个数之和满足给定target的下标,保存在一个数组中返回。一个简单的思路就是像冒泡排序一样利用两层遍历来
找到结果。复杂度为o(n^2).
     public int[] twoSum(int[] nums, int target) {
int [] result=new int [2];
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
if((nums[i]+nums[j])==target){ //遍历数组,找到满足的两个数的下标保存在数组中
result[0]= nums[i];
result[1]= nums[j];
}
}
}
if(result[0]==result[1]&&result[0]==0){ //这一步很多人不会注意,要判断原来初始化的数组中的数是否满足都不为0的要求
return null;
}else{
return result;
} }
其实这道题是一年前写的了,当时也就是为了通过而通过,所以没管复杂度的问题,其实这不是一个好习惯,今天第一次写博客又想了下,找到了更好的方法,利用
hashmap可以实现o(n):
     public static int[] twosum(int[] num,int target){
HashMap<Integer,Integer> map = new HashMap<>(); //构建hashmap
for (int i = 0;i<num.length;i++){
if (map.containsKey(target-num[i])){ //判断当前map中有木有与num[i]和为target的键,如果有则找到这对键值,
return new int[]{map.get(target-num[i]),i+1}; // 和当前下标组成结果
}else {
map.put(num[i],i);
}
}
return null;
}

  那么现在就算是完成这道题了吧,发现写博客真的不是一件那么简单的事,希望能坚持下来吧,最后来一张我gakki的美照纪念一下

  

 
 

 

LeetCode 题解(一):Two Sum的更多相关文章

  1. [LeetCode 题解]:Path Sum

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a bi ...

  2. LeetCode题解39.Combination Sum

    39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...

  3. 《LeetBook》LeetCode题解(1) : Two Sum[E]——哈希Map的应用

    001.Two Sum[E] Two SumE 题目 思路 1双重循环 2 排序 3 Hashmap 1.题目 Given an array of integers, return indices o ...

  4. LeetCode题解 #1 Two Sum

    在LeetCode做的第一到题 题目大意:给出n个数,在其中找出和为一个特定数的两个数. Input: numbers={2, 7, 11, 15}, target=9Output: index1=1 ...

  5. LeetCode 题解之 Two Sum

    1.题目描述 2.问题分析 使用hashTable 寻找,target  -  num[i] ,将时间复杂度降低到 O(n): 3.代码 vector<int> twoSum(vector ...

  6. LeetCode题解之 two sum 问题

    1.题目描述 2.题目分析 考虑使用hashMap的方式将数组中的每个元素和下表对应存储起来,然后遍历数组,计算target 和 数组中每个元素的差值,在hashMap中寻找,一直到找到最后一对. 3 ...

  7. [LeetCode 题解] Combination Sum

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a se ...

  8. [LeetCode 题解]: Two Sum

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an a ...

  9. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  10. 【LeetCode题解】136_只出现一次的数字

    目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...

随机推荐

  1. 实验楼-3-Linux用户及用户组

    获得自己用户名 $ who am i $ whoami pts/0 : 伪终端/序号 root操作:sudo Firstly,知道当前登录用户的密码:Secondly,当前用户在sudo用户组 添加新 ...

  2. Tcl与Design Compiler (九)——综合后的形式验证

    本文属于原创手打(有参考文献),如果有错,欢迎留言更正:此外,转载请标明出处 http://www.cnblogs.com/IClearner/  ,作者:IC_learner 这里来讲一下forma ...

  3. bootstrap file input 官方文档翻译

    file Input官方文档 中文翻译 file input 特性 1.这个插件会把简单的html文件变成一个更好用的文件选择输入控件,通过一个html的文件输入框,能兼容那些不支持jquery或js ...

  4. VB6/VBA中跟踪鼠标移出窗体控件事件(类模块成员函数指针CHooker类应用)

    一.关于起因 前几天发了一篇博文,是关于获取VB类模块成员函数指针的内容(http://www.cnblogs.com/alexywt/p/5880993.html):今天我就发一下我的应用实例. V ...

  5. Ubuntu上手动安装Kubernetes

    背景 两台Ubuntu16.04服务器:ip分别为192.168.56.160和192.168.56.161.. Kubernetes版本:1.5.5 Docker版本:1.12.6 etcd版本:2 ...

  6. android Instrumentoation 问答

    android Instrumentoation 问答   1.instrumentation是执行application instrumentation代码的基类.当应用程序运行的时候instrum ...

  7. 性能测试培训:tomcat性能调优方法

    性能测试培训:tomcat性能调优方法   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在poptest的loadrunner ...

  8. 老李分享:《Linux Shell脚本攻略》 要点(三)

    老李分享:<Linux Shell脚本攻略> 要点(三)   1.生产任意大小的文件 [root@localhost dd_test]#[root@localhost dd_test]# ...

  9. Grafana中多租户设置

    Grafana中通过设置不同的组织,以及将用户分配到不同组织,来做到多租户,类似门户的概念. Grafana默认是不允许非管理员用户创建新的组织的,这个可以通过修改配置文件以允许非管理员用户创建组织: ...

  10. IOS设备型号(原创)

    以下是我收集的ios目前为止移动设备型号,ipad air不知道,本人没有这款设备,求指导的给个回复,在这谢谢了 ///** ////////////////////   设备类型 字符串   /// ...