
leetcode刷题
王Clever
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
2. Add Two Numbers
class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode preNode = new ListNode(0); ListNode tail = preNode; int pre = 0; while (l1!=null&原创 2017-09-03 12:12:44 · 257 阅读 · 0 评论 -
51. N-Queens
一开始创建一个二维数组,每当放上皇后,就变为true,public class Solution { List<List<String >> lists = new ArrayList<>(); public List<List<String>> solveNQueens(int n) { boolean[][] map = new boolean[n][n];原创 2016-12-26 15:46:10 · 230 阅读 · 0 评论 -
417. Pacific Atlantic Water Flow
这道题起初我的思路,是深度搜索每一个点,由高往低,看最终是否到达边界,我现在的解参考了标准答案,思路是一样的,当代码也有很多不同,就是从边界开始深搜,用两个数组分别保存能连接p大洋和a大洋的点,如果这个点在这两个数组,这就是我们要找的点。public class Solution { public List<int[]> pacificAtlantic(int[][] matrix) {原创 2016-12-19 10:54:02 · 305 阅读 · 1 评论 -
16. 3Sum Closest
可以把这道题想象成在一条坐标轴上,求三个数之和到目标的距离的最小值,然后再用target加上或减去距离public class Solution { public int threeSumClosest(int[] nums, int target) { Arrays.sort(nums); int i = 0, j = nums.length;原创 2016-12-16 11:46:09 · 200 阅读 · 0 评论 -
Trapping Rain Water II
用优先队列,把边沿的墙全加进去,然后从最低的开始,看四周要不要补水。其他的和第一题很像public class Solution { class cell implements Comparable{ int x; int y; int height; public cell(int x, int y, int height) {原创 2016-12-09 10:44:48 · 418 阅读 · 0 评论 -
432. All O`one Data Structure
public class AllOne { /** Initialize your data structure here. */ public AllOne() { } int Inc = 0; HashMap<Integer,Set<String>> hm = new HashMap<>();//key是字符串的次数,set放这个数量的字符串 Has原创 2016-10-31 21:08:22 · 594 阅读 · 0 评论 -
文章标题
这题我写的有点复杂了public class Solution { public String multiply(String num1, String num2) { int len2 = num2.length(); int jinwei=0; String jia2 = ""; String result = mul(num1原创 2016-10-31 09:37:01 · 199 阅读 · 0 评论 -
381. Insert Delete GetRandom O(1) - Duplicates allowed
public class RandomizedCollection { HashMap<Integer,Integer> hashMap;/*前面的是插入的数字,后面是这个数字的数量*/ LinkedList<Integer> list ; /** Initialize your data structure here. */ public RandomizedCo原创 2016-10-30 14:54:03 · 215 阅读 · 0 评论 -
34. Search for a Range
很普通的二分查找吧public class Solution { public int[] searchRange(int[] nums, int target) { int start = 0; int middle = 0; int end = 0; int []xy = new int[2];//xy[0]是范围的第一个值,原创 2016-10-19 14:55:47 · 309 阅读 · 0 评论 -
深入分析 Java I/O 的工作机制
Java 的 I/O 类库的基本架构 I/O 问题是任何编程语言都无法回避的问题,可以说 I/O 问题是整个人机交互的核心问题,因为 I/O 是机器获取和交换信息的主要渠道。在当今这个数据大爆炸时代,I/O 问题尤其突出,很容易成为一个性能瓶颈。正因如此,所以 Java 在 I/O 上也一直在做持续的优化,如从 1.4 开始引入了 NIO,提升了 I/O 的性能。关于 NIO 我们将在后面详细介绍转载 2016-10-19 11:06:07 · 269 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock
这道题比较简单,从左到右扫描,每次都那当前数减去左边的最小数和profix比较,而最小数也很好找。2mspublic class Solution { public int maxProfit(int[] prices) { int min=99999; int profix =0; for(int i=0;i<prices.length;i+原创 2016-10-05 09:24:08 · 191 阅读 · 0 评论 -
402. Remove K Digits
我才用的是往队列加需要的字母public class Solution { public String removeKdigits(String num, int k) { Queue<Character> queue = new LinkedList<>(); int len = num.length(); int nowLen = len -原创 2016-11-08 16:56:19 · 225 阅读 · 0 评论 -
47. Permutations II
求当前排列的下一个排列的方法在我之前的博客中,排列是有序的 http://blog.youkuaiyun.com/github_31804537/article/details/52848094而这个是不停的找下一个排列,直到和第一个重复,就说明依旧全部遍历了public class Solution { public List<List<Integer>> permuteUnique(int[]原创 2016-10-18 14:09:08 · 199 阅读 · 0 评论 -
31. Next Permutation
这是别人的解释,自己打的代码,在当前序列中,从尾端往前寻找两个相邻元素,前一个记为first,后一个记为second,并且满足first 小于 second。然后再从尾端寻找另一个元素number,如果满足first 小于number,即将第first个元素与number元素对调,并将second元素之后(包括second)的所有元素颠倒排序,即求出下一个序列example: 6,3,4,9,8,原创 2016-10-18 13:22:29 · 265 阅读 · 0 评论 -
289. Game of Life
这道题仔细看懂题发现不难,注意每个细胞都是同时死或者生的,不能更新局部,必须要全局,于是就想出了用个新的培养细胞的板子newboard,来存放下一次状态的细胞。public class Solution { public void gameOfLife(int[][] board) { int []dx = {0,0,1,1,-1,-1,1,-1}; int原创 2016-10-04 13:53:23 · 237 阅读 · 0 评论 -
42. Trapping Rain Water
public class Solution { public int trap(int[] height) { int first = 0; int last = height.length-1; for(int i=0;i<height.length;i++){ if(height[i]!=0){first=i;brea原创 2016-11-08 14:18:16 · 193 阅读 · 0 评论 -
48. Rotate Image
这是我第一次一遍AC,这题看过一次解题思路,先左右对称,在斜对称 public class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for(int j=0;j<n;j++)//偶和奇数都一样的 for(int i=0;i<n/2;i原创 2016-10-17 23:47:16 · 159 阅读 · 0 评论 -
93. Restore IP Addresses
这道题用的还是搜索,关键是临界条件很重要public class Solution { public List<String> restoreIpAddresses(String s) { List<String>list = new ArrayList<>(); Set<String>set = new HashSet<>(); dfs(se原创 2017-01-05 12:17:36 · 181 阅读 · 0 评论 -
41. First Missing Positive
public class Solution { public int firstMissingPositive(int[] nums) { int count = 0; int start = 0; int end = nums.length - 1; // 这个把负数放到了最后的位置,其实没有必要,管负数,遇到负数直接跳过也行原创 2017-01-06 11:14:03 · 235 阅读 · 0 评论 -
131. Palindrome Partitioning
这里我用了搜索,比较常规,关键有一点,你删除之前加入的值,是按索引来删除,而不是删除值,不然顺序会出错。public class Solution { public List<List<String>> partition(String s) { List<List<String>>lists = new ArrayList<>(); List<String原创 2017-01-14 16:16:11 · 215 阅读 · 0 评论 -
20. Valid Parentheses
class Solution { public boolean isValid(String s) { char[]symbols = s.toCharArray(); Stack<Character> stack = new Stack<>(); for (int i = 0; i < symbols.length; i++) {原创 2017-09-03 09:58:48 · 273 阅读 · 0 评论 -
17. Letter Combinations of a Phone Number
class Solution { public List<String> letterCombinations(String digits) { LinkedList<String> ans = new LinkedList<String>(); String[] mapping = new String[] {"0", "1", "abc", "def", "原创 2017-09-03 09:40:24 · 336 阅读 · 0 评论 -
650. 2 Keys Keyboard
比如说2为2次才能得到,那么4需要4次,6需要5次,8需要6次。class Solution { public int minSteps(int n) { if(n==1)return 0; int[]dp= new int[n]; for (int i = 0; i < n; i++) { dp[i] = i+原创 2017-09-01 10:00:22 · 279 阅读 · 0 评论 -
670. Maximum Swap
需要注意的关键是相同的数字的处理class Solution { public int maximumSwap(int num) { char[]nums = String.valueOf(num).toCharArray(); for (int i = 0; i < nums.length; i++) { char max = num原创 2017-09-05 14:11:32 · 419 阅读 · 0 评论 -
647. Palindromic Substrings
class Solution { public int countSubstrings(String s) { boolean[][] dp = new boolean[s.length()][s.length()]; int result = 0; for (int i = 0; i < s.length(); i++) {原创 2017-08-19 10:24:18 · 169 阅读 · 0 评论 -
71. Simplify Path
这道题,比较简单,主要用链表来对路径进行操作,”.”的话,list就直接跳过,”..”的话,list删除尾节点,其他的都是放入尾节点public class Solution { public String simplifyPath(String path) { LinkedList<String > list = new LinkedList<String>();原创 2017-06-03 15:46:33 · 213 阅读 · 0 评论 -
540. Single Element in a Sorted Array
这道题从中间开始找,然后看左边长度是偶数,还是奇数,往是长度是奇数的那边找public class Solution { public int singleNonDuplicate(int[] nums) { int mid = nums.length/2; return findSingle(nums,0,nums.length-1); } p原创 2017-06-04 19:22:41 · 262 阅读 · 0 评论 -
57. Insert Interval
/** * Definition for an interval. * public class Interval { * int start; * int end; * Interval() { start = 0; end = 0; } * Interval(int s, int e) { start = s; end = e; } * } */原创 2017-03-10 17:55:56 · 231 阅读 · 0 评论 -
306. Additive Number
public class Solution { public boolean isAdditiveNumber(String num) { long first = 0;//这是第一个数 long second = 0;//这是第二个数 long third = 0;//这是第三个数 String values = "";原创 2017-03-07 16:13:26 · 222 阅读 · 0 评论 -
207. Course Schedule
构造了图结构,在外部用了dfs,最后计算hasCyclepublic class Solution { boolean hasCycle = false; Set set = new HashSet<Integer>(); class DGraph{ int n ; private boolean[] marked; privat原创 2017-02-26 11:20:28 · 345 阅读 · 0 评论 -
165. Compare Version Numbers
这道题通过“.”划分成数组,在一个一个数组比较public class Solution { public int compareVersion(String version1, String version2) { String []versionl1 = version1.split("\\."); String []versionl2 = version原创 2017-02-25 15:34:38 · 212 阅读 · 0 评论 -
23. Merge k Sorted Lists
17 ms, 完全的merge方法, 就是将一个数组,不停的分成两个数组,进行合并,基本类似于合并排序/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } *原创 2017-02-01 11:07:56 · 242 阅读 · 0 评论 -
208. Implement Trie (Prefix Tree)
public class Trie { class node{ int count = 0;// 用来删除,计数删除,虽然没有删除 node[] nodes = new node[26]; boolean isEnd = false; } private node root; /** Initialize your da原创 2017-01-25 14:49:59 · 201 阅读 · 0 评论 -
135. Candy
举一个例子,1,2,3,4,3,4,1 ,小朋友得到的糖分别是1,2,3,4,1,2,1 有特殊情况比如1,2,3,2,2,2小朋友得到的糖是1,2,3,1,1,1; 所以有三种情况一种是递增,一种是递减,一种是相等public class Solution { private int count; public int candy(int[] ratings) {原创 2017-01-21 14:34:58 · 217 阅读 · 0 评论 -
117. Populating Next Right Pointers in Each Node II
public class Solution { public void connect(TreeLinkNode root) { if (root == null) return; TreeLinkNode pre = root; TreeLinkNode cur = root; TreeLinkNode lastNode = r原创 2017-01-20 10:04:27 · 194 阅读 · 0 评论 -
54. Spiral Matrix
public class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> list = new ArrayList<>(); //上下左右边界 int down = matrix.length; if(down==0)retur原创 2017-01-01 10:34:00 · 203 阅读 · 0 评论 -
79. Word Search
dfs搜索,需要用到marked[][]标志,来解决已走过的路径。12mspublic class Solution { public boolean exist(char[][] board, String word) { int rows = board.length; int cols = board[0].length; int[][]原创 2016-10-03 14:00:21 · 206 阅读 · 0 评论 -
228. Summary Ranges
这道题我写代码写的优点乱了。定义一个最初的起始位置(也就是范围的第一个)first,然后循环,nums[i]=nums[i-1],就继续循环。直到nums[i]!=nums[i-1]在做处理,范围就是first到nums[i-1],但如果first==nums[i-1] ,就输出“first”,其他输出”first->nums[i-1]”,还有一种特殊情况也就是i==nums.length-1,末尾原创 2016-10-16 13:27:26 · 206 阅读 · 0 评论 -
128. Longest Consecutive Sequence
dp问题呀,虽然看了标签是union find,但一样可以用dp,只需要先排个序用dp就很简单了,24mspublic class Solution { public int longestConsecutive(int[] nums) { Set<Integer> set = new TreeSet<>(); List<Integer>list = new A原创 2016-10-11 09:33:38 · 185 阅读 · 0 评论 -
287. Find the Duplicate Number
其实这道题只要排好序,在完整的扫一遍数组public class Solution { public int findDuplicate(int[] nums) { Arrays.sort(nums); for(int i=0;i<=nums.length-2;i++){ if(nums[i]==nums[i+1])原创 2016-09-24 08:35:35 · 200 阅读 · 0 评论