LeetCode-Java代码
cravingszy
想要更好的东南小渣渣、
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
58. Length of Last Word
题目:Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string. If the last word does not exist, return 0.For example, Given原创 2016-03-11 11:26:07 · 222 阅读 · 0 评论 -
64. Minimum Path Sum
Minimum Path SumGiven a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either dow原创 2016-03-28 11:21:06 · 191 阅读 · 0 评论 -
53. Maximum Subarray
Maximum Subarray题意:给定一个数组 找出和最大的子序列 For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6.代码其实题意很简单 使用动态规划即可public class Solution { pu原创 2016-04-26 09:28:48 · 212 阅读 · 0 评论 -
44. Wildcard Matching
Wildcard Matching44.Wildcard Matching和10.Regular Expression Matching这两个题目咋一看以为是一样的,其实不是,第一题其实是通配符匹配,即是说星号可以匹配任何的子字符串,而第二题其实是正则表达式匹配,即是说星号表示它之前的字符可以出现任意多次(包括0),这其实就是编译原理龙书上讲的克林闭包。 Some examples: isMat原创 2016-03-28 19:15:41 · 249 阅读 · 0 评论 -
63. Unique Paths II
Unique Paths IIThere is one obstacle in the middle of a 3x3 grid as illustrated below. [ [0,0,0], [0,1,0], [0,0,0] ] The total number of unique paths is 2. 如果加了障碍会怎么样 其实也同样使用动态规划 多增加一步原创 2016-03-27 16:20:41 · 295 阅读 · 0 评论 -
43. Multiply Strings
Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a string. 给俩个字符串的数字,计算想乘的结果public class Solution { public String multiply(String num1, String num2原创 2016-03-21 14:38:24 · 197 阅读 · 0 评论 -
83. Remove Duplicates from Sorted List
Remove Duplicates from Sorted List删除重复的 Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, retu原创 2016-04-10 11:11:48 · 271 阅读 · 0 评论 -
82. Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List IIGiven a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example, Given 1->2->3->3->4->4-原创 2016-04-10 15:00:34 · 294 阅读 · 0 评论 -
54. Spiral Matrix
螺旋数组Spiral Matrix题意: 使用递归,一次扫描一整圈,然后用x,y记录这个圈的左上角,递归完了都+1。rows, cols记录还没扫的有多少。思想比较简单,但相当容易出错。提交了好多次。 注意:1. 扫描第一行跟最后一行要扫到底部为止,而扫描左列和右列只需要扫中间的。1 2 3 4 5 6 7 8 9 10 11 12例如以上例子: 你要 先扫1234, 然原创 2016-04-27 09:48:57 · 270 阅读 · 0 评论 -
80. Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array IIWhat if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5 可以保留2次重复的代码public cl原创 2016-04-11 11:04:50 · 231 阅读 · 0 评论 -
107. Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal IIFor example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its bottom-up level order traversal as: [ [15,7], [9,2原创 2016-04-13 09:22:03 · 232 阅读 · 0 评论 -
62. Unique Paths
Unique Paths从起点走到终点有多少种方法 思路使用简单的动态规划: 首先对两边进行赋值 因为机器人不能向上走 或者向左走 所以两边的格子里都是1 其次再通过动态规划【F(m)(n)=F(m-1)(n)+F(m)(n-1)】计算走到最后一格有多少种方法代码public class Solution { public int uniquePaths(int m, int n)原创 2016-03-27 16:00:45 · 217 阅读 · 0 评论 -
66. Plus One
Plus One题解:一个整数按位存储于一个int数组中,排列顺序为:最高位在array[0] ,最低位在[n-1],例如:98,存储为:array[0]=9; array[1]=8;思路:从数组的最后一位开始加1,需要考虑进位,如果到[0]位之后仍然有进位存在,需要新开一个长度为(n.length + 1)的数组,拷贝原来的数组。代码:public class Solution { pub原创 2016-03-23 09:29:08 · 235 阅读 · 0 评论 -
127. Word Ladder
Word Ladder 使用了bfs(宽度优先搜索算法) 随便看看 代码复制别人的~~~代码public class Solution { public int ladderLength(String beginWord, String endWord, Set<String> wordList) { Set<String> beginSet = new HashSet<原创 2016-04-05 10:47:36 · 349 阅读 · 0 评论 -
67. Add Binary
Add Binary给定两个二进制字符串,将它们相加并将结果输出 For example, a = “11” b = “1” Return “100”代码public class Solution { public String addBinary(String a, String b) { int alength = a.length(); int原创 2016-03-23 15:22:42 · 271 阅读 · 0 评论 -
55. Jump Game
Jump GameGiven an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determi原创 2016-03-24 10:39:49 · 243 阅读 · 0 评论 -
45. Jump Game II
Jump Game II算出能跳到最后一步的最小的步数 For example: Given array A = [2,3,1,1,4] The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)代码同样用贪心原创 2016-03-24 11:31:39 · 219 阅读 · 0 评论 -
51. N-Queens
N-Queens皇后问题 解题思路使用回溯法解决N皇后问题,是常见的解决方法。分N步放置皇后,由于皇后之间不能同行、同列、同斜线,所以每次放置皇后的时候,都要考虑是否与已有的皇后“冲突”,如果冲突,则改变位置,直至所有皇后放置完毕。代码原创 2016-04-06 17:21:09 · 361 阅读 · 0 评论 -
52. N-Queens II
N-Queens II、跟51. N-Queens I条件一样 只是此题求出有多少个解即可代码public class Solution { Set<Integer> cols = new HashSet<Integer>(); Set<Integer> diags1 = new HashSet<Integer>(); Set<Integer> diags2 = new H原创 2016-04-07 10:42:40 · 235 阅读 · 0 评论 -
39. Combination Sum
39. Combination Sum题意: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen fr原创 2016-03-14 20:26:58 · 245 阅读 · 0 评论 -
102. Binary Tree Level Order Traversal
Binary Tree Level Order TraversalGiven a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}原创 2016-04-12 14:14:49 · 240 阅读 · 0 评论 -
59. Spiral Matrix II
Spiral Matrix II对比上一题 此题的题意: 其实跟上一题一样 同样使用四个for循环 主要注意不要搞混变量 不然很烦代码public class Solution { public int[][] generateMatrix(int n) { int[][] ret = new int[n][n]; int top = 0,lef原创 2016-04-28 09:33:27 · 241 阅读 · 0 评论 -
111. Minimum Depth of Binary Tree
Minimum Depth of Binary Tree题意: Given a binary tree, find its minimum depth.代码:public class Solution { public int minDepth(TreeNode root) { if(root == null) return 0; int left = mi原创 2016-04-15 11:22:22 · 235 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree
夏天到了 不想学习 写个简单的代码清醒一下 然后突然发现然并卵 这比装的自己都看不下去了 o(︶︿︶)o 唉 不说了 还是乖乖去改论文吧。。。Maximum Depth of Binary TreeGiven a binary tree, find its maximum depth.代码public class Solution { public int maxDepth(原创 2016-04-18 09:25:59 · 187 阅读 · 0 评论 -
110. Balanced Binary Tree
Balanced Binary Tree判断是不是平衡二叉树代码public class Solution { private int helper(TreeNode root, int height){ if (root == null) { return height; } int leftTree =原创 2016-05-19 11:12:03 · 297 阅读 · 0 评论 -
61. Rotate List
Rotate List反转列表 代码public class Solution { public ListNode rotateRight(ListNode head, int k) { if(head == null || k == 0){ return head; } ListNode p = head;原创 2016-06-01 10:27:09 · 324 阅读 · 0 评论 -
92. Reverse Linked List II
Reverse Linked List II代码public class Solution { public ListNode reverseBetween(ListNode head, int m, int n) { if(head == null){ return null; } ListNode dummy = n原创 2016-05-09 10:38:28 · 224 阅读 · 0 评论 -
75. Sort Colors
Sort Colors**题意:**Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will us原创 2016-04-25 09:54:53 · 327 阅读 · 0 评论 -
89. Gray Code
格雷码For example, given n = 2, return [0,1,3,2]. 代码public class Solution { public List<Integer> grayCode(int n) { List<Integer> result = new LinkedList<>(); for (int i = 0; i < 1<<n;原创 2016-06-02 11:07:42 · 255 阅读 · 0 评论 -
74. Search a 2D Matrix
Search a 2D Matrix代码使用二分法public class Solution { public boolean searchMatrix(int[][] matrix, int target) { int col_num = matrix[0].length; int row_num = matrix.length; int be原创 2016-05-20 10:27:43 · 254 阅读 · 0 评论 -
57. Insert Interval
Insert Interval:题意:主要考虑以下情况: 如果新区间的end < 当前区间的start,不用找下去了,把新区间插入到当前区间的前面,然后返回。 如果当前区间的end小于新区间的start,继续遍历找下一个区间。 如果当前区间和新区间发生重合,则start取两者最小的start,end取两者最大的end,生成一个新的区间。 继续遍历。 如果遍历一直到末尾也没发生区间重合,就原创 2016-05-23 10:34:28 · 255 阅读 · 0 评论 -
60. Permutation Sequence
全排列Permutation Sequence这题当时就没看懂 求出全排列中的第k个排列组合 代码//到现在还不是很懂 只是自己觉得这个算法很diao 所以拷贝下来以后看(也不知道会不会看 ….)public class Solution { int[] nums; boolean[] mark; public int find(int n,int k){ int原创 2016-06-06 10:58:21 · 307 阅读 · 0 评论 -
86. Partition List
Partition List 题意:给定一个单链表和一个x,把链表中小于x的放到前面,大于等于x的放到后面,每部分元素的原始相对位置不变。思路:其实很简单,遍历一遍链表,把小于x的都挂到head1后,把大于等于x的都放到head2后,最后再把大于等于的链表挂到小于链表的后面就可以了。代码public class Solution { public ListNode partition(Li原创 2016-06-07 10:34:09 · 377 阅读 · 0 评论 -
100. Same Tree
Same TreeGiven two binary trees, write a function to check if they are equal or not.代码:public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if(p == null && q == null)原创 2016-04-22 11:15:43 · 221 阅读 · 0 评论 -
98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).题解:代码public class Solution { List<Integer> list = new ArrayList<Integer>(); public boolean isValidBST(TreeNode root) {原创 2016-05-30 09:45:19 · 346 阅读 · 0 评论 -
118. Pascal's Triangle
杨辉三角代码public class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> res = new ArrayList<List<Integer>>(); List<Integer> row, pre = null; for原创 2016-05-26 10:23:35 · 239 阅读 · 0 评论 -
48. Rotate Image
Rotate Image分析:给定一个二维数组a[n][n]顺时针旋转90度,要解决这个问题,无疑,第一件事儿就是找规律。 代码public class Solution { public void rotate(int[][] matrix) { for(int i = 0; i < matrix.length; i++){ for(int j =原创 2016-04-19 09:59:10 · 214 阅读 · 0 评论 -
112. Path Sum
Path SumGiven a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum 代码public class Solution { public boolean原创 2016-05-04 08:52:13 · 237 阅读 · 0 评论 -
113. Path Sum II
Path Sum II代码public class Solution { List<List<Integer>> res = new ArrayList<List<Integer>>(); public List<List<Integer>> pathSum(TreeNode root, int sum) { if(root == null){原创 2016-05-04 10:35:49 · 255 阅读 · 0 评论 -
65. Valid Number
Valid Number此题有点没怎么懂、、、、 代码public class Solution { public boolean isNumber(String s) { s = s.trim(); boolean pointSeen = false;// 不知道为什么要定义4个 没想明白 boolean eSeen = false;原创 2016-05-15 10:12:40 · 245 阅读 · 0 评论
分享