leetcode算法笔记1
来源:互联网 发布:淘宝代购lv是真的吗 编辑:程序博客网 时间:2024/06/10 23:22
leet code oj 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].
利用hashmap,可降低时间复杂度为 O(n)
public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int complement = target - nums[i]; if (map.containsKey(complement)) { return new int[] { map.get(complement), i }; } map.put(nums[i], i); } throw new IllegalArgumentException("No two sum solution");}
一次循环,将数据存入 求target的补是否在Hashmap中,同时
阅读全文
0 0
- leetcode算法笔记1
- 46. Permutations --- leetcode 算法笔记
- 算法学习笔记[LeetCode]Valid Parentheses
- 算法学习笔记[LeetCode]Min Stack
- 算法学习笔记[LeetCode][ZigZag Conversion]
- 13. Roman to Integer leetcode算法笔记
- 27. Remove Element ---leetcode算法笔记
- 48. Rotate Image ---leetcode算法笔记
- leetcode学习笔记1
- LeetCode算法题1-10
- 数据结构与算法之leetcode刷题笔记
- 8. String to Integer (atoi) leetcode算法笔记
- 83. Remove Duplicates from Sorted List leetcode算法笔记
- 75. Sort Colors --- one-pass algorithm --- leetcode算法笔记
- 3. Longest Substring Without Repeating Characters --- leetcode算法笔记
- 算法笔记 ---- 学习笔记 1
- LeetCode算法
- LeetCode 算法
- Java
- markdown发生HTML渲染组件出错的解决方案
- [二分+LCA+差分]【NOIP2015D2T3】运输计划 题解
- C语言学习系列1-helloworld示例
- Effect(七)—— BuffEffect
- leetcode算法笔记1
- oracle之 Oracle归档日志管理
- linux vsftpd 升级步骤
- Java IO流的介绍
- java打印3位的水仙花数
- 23. Merge k Sorted Lists
- FTPClient连接FTP服务器
- 《编程之美》
- php获取中文字符拼音首字母和城市的排序