LeetCode
井底的笨鸟
Stay hungry,stay foolish.
展开
-
链表——按k个结点一组来反转链表
题目:按k个结点一组来反转链表思路:运用反转链表的通法reverse,对链表进行循环,当计数长度k不时,指针继续前进;当计数长度到达k时,将该组首尾节点first和node作为参数传入翻转函数reverse进行翻转,然后重新拼接到原链表中。直至到达链表末尾。代码如下:/** * Definition for singly-linked list. * publi原创 2016-05-10 10:46:18 · 1435 阅读 · 0 评论 -
链表——成对的反转链表
题目:swap-nodes-in-pairs 一对一对的反转链表内的结点For example,Given1->2->3->4, return 2->1->4->3.思路:“按K个结点一组反转链表”当k为2时的特殊情况。代码如下:/** * Definition for singly-linked list. * public class原创 2016-05-10 11:01:12 · 901 阅读 · 0 评论 -
链表——合并K个排序的链表
题目:merge-k-sorted-lists思路:合并两个排序链表升级版,那就两两合并!代码如下:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { *原创 2016-05-10 11:26:03 · 574 阅读 · 0 评论 -
树——sum-root-to-leaf-numbers
题目:二叉树每个结点包含0-9的数字,例如一条root到leaf的路径为h1->2->3,则该路径和为123,求该二叉树总的路径和。,For example, 1 / \ 2 3The root-to-leaf path1->2represents the number12.The root-to-leaf path1->3原创 2016-05-10 17:11:31 · 344 阅读 · 0 评论 -
树——populating-next-right-pointers-in-each-node(层序遍历变型)
题目:节点中多了一个next引用指向该节点的右边节点,若没有右边节点则next=null.题目假设树是完全二叉树,且只能使用O(1)辅助空间。 struct TreeLinkNode{ TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }例如:原创 2016-05-10 22:29:25 · 474 阅读 · 0 评论 -
树——从最下层向上打印二叉树(层序遍历扩展)
题目:要求按深度从下到上访问二叉树,每层顺序从左到右。例如:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its bottom-up level order traversal as:[ [15,7] [9,20], [3],原创 2016-05-11 14:01:06 · 405 阅读 · 0 评论 -
树——二叉树层序遍历
方法一:广度优先搜索(BFS),用一个辅助动态数组ArrayList,cur表示当前层结点个数,next表示下一层结点个数;若只需要打印某层结点,则只需再设一个变量表示层数即可。代码如下:import java.util.*;public class Solution { public void levelOrder(TreeNode root) { if(root...原创 2016-05-11 10:50:48 · 761 阅读 · 0 评论 -
树——由中序和前序,中序和后序序列重建二叉树
题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。一、由前序和中序重构代码如下:/** * Definition for binary tree * public class TreeNode原创 2016-05-11 14:58:12 · 662 阅读 · 0 评论 -
树——按“之”字形打印二叉树(层序遍历变型)
题目描述请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。思路:用两个stack依次保存相邻的两层。代码如下:*public class TreeNode { int val = 0; TreeNode left = null; TreeNode righ原创 2016-05-11 10:41:33 · 1488 阅读 · 0 评论 -
树——对称的二叉树
题目:判断一棵二叉树是否对称判断两棵二叉树是否相同:public boolean isSameTree(TreeNode p, TreeNode q) { if(p == null && q == null) return true; if(p == null || q == null) return原创 2016-05-11 16:34:55 · 275 阅读 · 0 评论 -
树——判断是否为二叉搜索树
题目:Given a binary tree, determine if it is a valid binary search tree (BST).二叉搜索树判定:左子树上的结点的值都比父节点小,右子树都比父节点大。且左右子树也分别为二叉搜索树。方法一:利用BST性质,中序遍历为递增序列。import java.util.*;public class Solution { publi...原创 2016-05-11 19:56:27 · 371 阅读 · 0 评论 -
树——recover-binary-search-tree
题目:二叉搜索树BST中有两个结点的值互相放错了位置,找出并交换它们。要求:只能使用O(1)空间复杂度。思路:若依据BST性质,使用ArrayList存储其中序遍历,可以很方便的找出题目要求的两个值,但是空间复杂度为O(n)。这里有两种情况需要注意:1.两个放错位置的结点相邻,如 中序遍历为:1,2,4,3,5. 则pre.val>cur.val的情况只原创 2016-05-11 21:06:09 · 482 阅读 · 0 评论 -
String——zigzag-conversion
题目:例如给定字符串和行数String s="PAYPALISHIRING" 和nRows == 3;P A H NA P L S I I GY I Rreturn"PAHNAPLSIIGYIR".这题里包含的关于String[] str=new Srting[1];字符串数组的一个小知识点。如下:public static voi原创 2016-05-12 20:59:43 · 295 阅读 · 0 评论 -
valid-palindrome
验证一个字符串是否是回文字符串。Famous examples include "Amore, Roma", "A man, a plan, a canal: Panama" and "No 'x' in 'Nixon'"。 判读一个字符串是否是回文,一种方法可以将字符串倒置然后和原字符串进行比较。这里采用一种类似字符串翻转的方法,通过从前后两个方向来比较判断是否是回文。本题中的有效字原创 2016-05-27 14:58:46 · 393 阅读 · 0 评论 -
String——最后的单词的长度
题目:字符串包含大小写字母和' '空格,返回最后一个单词的长度。For example, Given s ="Hello World",return5.方法一:利用split函数。public class Solution { public int lengthOfLastWord(String s) { if原创 2016-05-12 11:29:57 · 378 阅读 · 0 评论 -
longest-common-prefix 求字符串数组的最长公共前缀
2个字符串的最长公共前缀,其长度肯定不会超过最短的字符串的长度,设最短的字符串长度为n,那么只要比较这2个字符串的前n个字符即可。如此得出这2个字符串的最长公共前缀prefix后,再拿prefix作为新的字符串和数组中的下一个字符串比较,方法同上。需要注意的是,如果数组中的某个字符串长度为0,或者求得的当前最长公共前缀的长度为0,就直接返回空字串。public class Solution原创 2016-05-27 21:21:53 · 340 阅读 · 0 评论 -
pascals-triangle,pascals-triangle-ii
题目描述 :pascals-triangleGiven numRows, generate the firstnumRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1原创 2016-05-28 11:24:30 · 438 阅读 · 0 评论 -
链表——删除链表中倒数第n个结点(时间复杂度为O (n))
题目:删除链表中倒数第n个结点(时间复杂度为O (n)) 原始列表: 1->2->3->4->5, and n = 2. 删除后: 1->2->3->5. 这里所给出的n满足 n>=0&&n思路:找到倒数第(n+1)个结点,就能很容易地删除倒数第n个结点。/** * Definition for s原创 2016-05-09 21:12:13 · 821 阅读 · 0 评论 -
链表——将两个链表上的数字相加(考虑进位)并将和放入新链表
题目:给予两个链表,每个节点中有一个0-9的数字,将相应位置上的数字相加并进位,用新的链表存储其和。例如:Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8思路:可能的情况有:1.两个链表first和second都没有到表尾;2.一个到表尾,另一个没有;原创 2016-05-09 19:47:43 · 3897 阅读 · 0 评论 -
链表——循环右移链表的后K个结点
题目:输入k和链表的头结点,循环右移链表的后K个结点。For example:Given1->2->3->4->5->NULLand k =2,return4->5->1->2->3->NULL.思路:1.首先要找链表的倒数第K个结点;2.因循环右移的K个结点仍是按原来顺序排列,可考虑用一个先进先出的容器即队列将后K个结点存储,依次连接在链表首处;原创 2016-05-09 18:49:25 · 2666 阅读 · 1 评论 -
数组——将排序数组转换为平衡二叉搜索树
题目描述:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.和有序链表化为BST方法一样,递归求解;注:中间必须要两个中间结点的后一个!public class Solution { public TreeNod原创 2016-05-16 20:21:25 · 669 阅读 · 0 评论 -
数组——sort colors
题意:对一个容量为n含有红色(用0表示),白色(用1表示),和蓝色(用2表示)的数组按升序排序。。。 public void sortColors(int[] A) { if(A == null||A.length == 0) return; //计数排序; int red=0;int white=0;int blue原创 2016-05-16 20:26:13 · 357 阅读 · 0 评论 -
数组——二维数组的查找
题目:在一个二维数组上,每一行从左到右递增,每一列从上到下递增,高效的查找二维数组中是否有所要查找的数字target。For example,Consider the following matrix:[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]]Given target =3, ret原创 2016-05-16 20:47:14 · 262 阅读 · 0 评论 -
数组——remove-duplicates-from-sorted-array-ii
题目:移除排序数组中多余的元素,使每个元素最多出现两次;For example,Given sorted array A =[1,1,1,2,2,3],Your function should return length =5, and A is now[1,1,2,2,3].题解:使用双下标,一个下标用来对重复次数少于等于2的数赋值(原数组中)原创 2016-05-16 21:28:01 · 312 阅读 · 0 评论 -
回溯法——电话号码代表字符组合
题目描述Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.原创 2016-05-29 15:06:21 · 789 阅读 · 0 评论 -
回溯法——permutation、permutation II数组数字的全排列
题目描述:permutationGiven a collection of numbers, return all possible permutations.For example,[1,2,3]have the following permutations:[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], and[3,2,1].原创 2016-05-29 16:25:41 · 642 阅读 · 0 评论 -
next-permutation
题目描述:找到排序数组的下一个排序例如:1,2,3→1,3,23,2,1→1,2,31,1,5→1,5,1字典序排列把升序的排列(当然,也可以实现为降序)作为当前排列开始,然后依次计算当前排列的下一个字典序排列。对当前排列从后向前扫描,找到一对为升序的相邻元素,记为i和j(i 到第一个大于i的元素k,交换i和k,然后对从j开始到结束的子序原创 2016-05-29 16:52:46 · 368 阅读 · 0 评论 -
回溯法——permutation-sequence 返回第k个排序序列
题目描述The set[1,2,3,…,n]contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132"原创 2016-05-29 17:28:51 · 801 阅读 · 1 评论 -
数组——merge-intervals
题目描述:Given a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18]./** * Definition for an interv原创 2016-05-17 16:16:37 · 347 阅读 · 0 评论 -
数组——reverse integer和 palindrome-number
判断一个数字是否是回文数字。方法一:用字符串的方法,StringBuilder 中的reverse()方法。 public boolean isPalindrome(int x) { String number=String.valueOf(x); StringBuilder sb=new StringBuilder(number); if(原创 2016-05-17 17:05:05 · 387 阅读 · 0 评论 -
链表——partition list
题目:给定一个单链表和一个x,把链表中小于x的放到前面,大于等于x的放到后面,每部分元素的原始相对位置不变。思路:遍历一遍链表,把小于x的结点都放在lesshead后,把大于等于x的都放到biggerhead后,最后再把大于等于的链表接在小于链表的后面。代码如下:/** * Definition for singly-linked list.原创 2016-05-09 15:29:52 · 333 阅读 · 0 评论 -
链表——删除链表中的重复结点
For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3.代码如下:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListN...原创 2016-05-09 14:55:29 · 352 阅读 · 0 评论 -
链表——(循环和递归)合并两个排序链表
题目:合并两个递增排序链表,使新链表仍然按照递增排序。方法一:基于递归的方法,链表first和链表second各有m和n个结点,新链表的头结点为两个链表中头结点较小的一个,当找到该头结点时(假设为first的头结点),仍需对first的m-1个结点和second的n个结点合并。可以看出,子问题和原问题相同,因此可以利用递归解决。代码如下:/** * Definit原创 2016-05-09 16:45:30 · 3857 阅读 · 0 评论 -
valid-sudoku判断是否是有效数独
题目描述题意:判断一个9*9的矩阵的每一行,每一列,每一个小九宫格是否没有重复的数字,数字范围为“1-9”.A partially filled sudoku which is valid.import java.util.*;public class Solution { public boolean isValidSudoku(char[][] board) {原创 2016-06-01 21:25:03 · 1057 阅读 · 0 评论 -
迭代器——insert-interval
题目描述Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their s原创 2016-06-07 12:29:06 · 517 阅读 · 0 评论 -
回溯法——combinations同数组的所有组合
题目描述Given two integers n and k, return all possible combinations of knumbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1原创 2016-06-07 13:33:58 · 655 阅读 · 0 评论 -
动态规划——triangle空间复杂度O(n)
仍是数字三角形问题,求从三角形顶端到最底层最小路径和。题目要求空间复杂度O(n),那么就要将二维数组压缩为一维。因DP中历史状态只能通过当前状态影响下一个状态,所以压缩数组是可行的。数字三角形如下:[ [2], [3,4], [6,5,7], [4,1,8,3]]import java.util.*;public class Solut原创 2016-05-21 18:55:36 · 1981 阅读 · 1 评论 -
动态规划——candy
题目描述n个小朋友站成一排,根据他们的得分分发糖果,得分高的小朋友要比旁边得分低的小朋友得到的糖果多,每个小朋友至少得到一枚糖果,问最少要准备多少糖果?方法:先从左到右扫描一遍,使得右边比左边得分高的小朋友糖果数比左边多1(尽可能的少);再从右到左扫描一遍,使得左边比右边得分高的小朋友糖果数比右边多(这里要注意,从右向左比较时,只有当左边小朋友比右边的分高且得到的糖果少于等于右边时原创 2016-05-21 21:43:41 · 682 阅读 · 0 评论 -
贪心法、动态规划——jump-game,jump-gameII
jump-game题目描述Given 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 a原创 2016-06-08 16:12:58 · 869 阅读 · 0 评论 -
栈——largest-rectangle-in-histogram求柱形图中的最大矩形面积
题目描述Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where wid原创 2016-06-03 12:43:22 · 1922 阅读 · 0 评论