数组-leetcode 1. Two Sum
2017-07-14 00:45
555 查看
原题链接:Two Sum
题解:
旁白:因业务需要转java,特用java刷lc,觉得写习惯了c++写java很不方便,还是觉得c++用起来舒服,可能是习惯了c++并且对java还不熟吧。
题解:
public class Solution { public int[] twoSum(int[] nums, int target) { /* Time Complexity:O(n) Space Complexity:O(n) */ int[] res=new int[2]; if(nums==null || nums.length<2)return res; HashMap<Integer,Integer>hashMap=new HashMap<>(); for(int i=0;i<nums.length;i++){ if(hashMap.containsKey(nums[i])){ res[0]=hashMap.get(nums[i]); res[1]=i; break; } else{ hashMap.put(target-nums[i],i); } } return res; } }
旁白:因业务需要转java,特用java刷lc,觉得写习惯了c++写java很不方便,还是觉得c++用起来舒服,可能是习惯了c++并且对java还不熟吧。
相关文章推荐
- 【LeetCode 1_数组_哈希表】Two Sum
- leetcode-1 Two Sum 找到数组中两数字和为指定和
- LeetCode-two sum:python解答数组问题
- Leetcode Two Sum (数组)
- Two Sum 数组中找两个之和为sum@LeetCode
- [leetcode] 【数组】1. Two Sum
- leetcode-1 Two Sum 找到数组中两数字和为指定和
- leetcode 46: Two Sum
- LeetCode-238. Product of Array Except Self (JAVA)(构建乘积数组)
- leetCode 1. Two Sum 数组
- LeetCode(40) Median of Two Sorted Arrays (两排序数组中位数)
- LeetCode Two Sum
- leetcode 1. Two Sum (C语言)12
- leetcode数组之Climbing Stairs(同剑指offer T9)
- leetCode 189. Rotate Array 数组
- leetcode--Two Sum
- LeetCode-Remove Duplicates from Sorted Array-从有序数组移除重复-简单逻辑
- [LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法
- Two Sum - LeetCode 1
- Leetcode 4 Median of Two Sorted Arrays 两排序数组的中位数