leetcode
文章平均质量分 74
吕式不爽
这个作者很懒,什么都没留下…
展开
-
leetcode: LRU Cache
import java.util.Map;import java.util.HashMap;public class LRUCache { Map cacheMap;ListNode head;ListNode tail;public int cap;public int last; public LRUCache(int capac原创 2014-10-10 20:04:21 · 535 阅读 · 0 评论 -
leetcode: Permutations/Permutations II
按照字典序依次写,求一个序列的下一字典序序列:A[0] A[0] A[0] A[0]原创 2014-05-06 12:23:18 · 411 阅读 · 0 评论 -
leetcode:Wildcard Matching
public class Solution { public static boolean isMatch(String s, String p) { if(s.equals("")) return p.equals("")?true:false; if(p.equals("")) return false; //System.out.prin原创 2014-05-05 11:15:33 · 535 阅读 · 0 评论 -
leetcode: Combination Sum
写得比较复杂,深搜吧 也没有什么很好地办法。public class Solution { public ArrayList> combinationSum(int[] candidates, int target) {Arrays.sort(candidates);return DFS(candidates,target,0,candidates.length);原创 2014-03-22 21:55:27 · 420 阅读 · 0 评论 -
leetcode: Trapping Rain Water
好久没有做leetcode了,因为最近心情不好。这道题还是左右指针,public class Solution { public int trap(int[] A) { int sum=0; int begin=0; int i,l,r,min; int n=A.length; if(n==0)原创 2014-05-04 14:53:52 · 415 阅读 · 0 评论 -
leetcode: Count and Say
这个技术主要用在文件压缩吧,比如111112,有5个1和1个2,于是变为5112.理解题意比较重要。递归实现。public class Solution { public String countAndSay(int n) { String rst=""; if(n==1) { rst+="1";原创 2014-03-19 20:29:03 · 412 阅读 · 0 评论 -
leetcode:Sudoku Solver
算法打死没想出来,看了看别人的代码,才发现是暴力枚举(递归)首先找一个没有填的空,然后填入1-9,判断填入之后的数是否有冲突,没有就继续递归。public class Solution { public void solveSudoku(char[][] board) {msolveSudoku(board); }public boolean msolveS原创 2014-03-18 21:45:08 · 379 阅读 · 0 评论 -
leetcode: Valid Sudoku
根据坐标找到对应的行,列,块即可。设置三个标志矩阵。public class Solution { public boolean isValidSudoku(char[][] board) { int[][] rowflag=new int[9][9]; int[][] colflag=new int[9][9]; int[]原创 2014-03-18 20:14:48 · 373 阅读 · 0 评论 -
leetcode: Search Insert Position
也是二分法做的public class Solution { public int searchInsert(int[] A, int target) { return msearch(A,target,0,A.length-1); } public static int msearch(int[] A, int target, in原创 2014-03-18 19:57:01 · 439 阅读 · 0 评论 -
leetcode:Search for a Range
二分法:public class Solution { public int[] searchRange(int[] A, int target) { return msearch(A,target,0,A.length-1); } public static int[] msearch(int[] A, int target, int begi原创 2014-03-17 21:57:13 · 409 阅读 · 0 评论 -
leetcode: Search in Rotated Sorted Array
二分法递归:有两种情况:(1)A[mid](2)A[mid]>=A[end]如3456789012根据target和mid,begin,end的大小关系,去二分原数组public class Solution { public static int search(int[] A, int target) { return msearch(原创 2014-03-17 21:00:53 · 358 阅读 · 0 评论 -
Combination Sum II
不是two sum,所以还是深搜每个数的搜索深度是它重复的次数,例如有两个1,搜索就是0,1,2三次其他和combination sum相同public class Solution { public ArrayList> combinationSum2(int[] num, int target) { Arrays.sort(num);return DFS原创 2014-03-27 22:12:52 · 374 阅读 · 0 评论 -
leetcode: Longest Valid Parentheses
动态规划:F[i]表示末尾到第i个括号的最大匹配括号数。last=i-1-F[i-1],表示上一个不匹配的地方,要么是这样:****(*****)就匹配了,那么F[i]=f[last-1]+2+F[i-1]要么就是不匹配,F[i]=0public class Solution { public int longestValidParentheses(String原创 2014-03-17 20:29:39 · 342 阅读 · 0 评论 -
leetcode:Reverse Nodes in k-Group
递归,每次取k个元素。然后对这k个元素处理,返回{k个元素}+reverseKGroup(tail,k)比如1-2-3-4-5-6,k=4,那就先处理1-2-3-4,tail=5,5-6后续再处理。处理过程是1(head,p1)-2(p2)-3-4,p1是当前最左边的元素,head是原来的头,p2是原来头下面的元素。然后把p2移到p1左边:2(p1)-1(head)-3(p2)-4原创 2014-03-10 16:39:53 · 430 阅读 · 0 评论 -
leetcode:Jump Game II
public class Solution { public int jump(int[] A) { int n=A.length; int[] maxstep=new int[n]; int i,j,max,tmp;; if(n<2) return 0; maxstep[0]=0; //0 for(i原创 2014-05-05 23:16:16 · 402 阅读 · 0 评论 -
leetcode: Rotate Image
n X n的考虑到:第一象限的点全原创 2014-05-06 19:16:31 · 504 阅读 · 0 评论 -
leetcode: Rotate List
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public原创 2014-06-19 15:13:24 · 475 阅读 · 0 评论 -
leetcode: Unique Paths
public class Solution { public int uniquePaths(int m, int n) { int i; double result=1.0; n=n-1; m=m+n-1; if(m-n<n) n=m-n; for(i=1;i<=n;i++)原创 2014-07-01 22:06:20 · 436 阅读 · 0 评论 -
leetcode: Permutation Sequence
这个题要求全排列的第几个。首先我们知道,A(n,m)=n!/(n-m)原创 2014-06-11 15:26:55 · 497 阅读 · 0 评论 -
leetcode: Merge Intervals
/** * 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; } * } */原创 2014-05-10 22:08:59 · 424 阅读 · 0 评论 -
leetcode: Insert Interval
因为原来的intervals是不想交且的,所以对原来的每一个区间,和要插入的区间对比,考虑如下情况:绿色表示当前的intervals[i],黑色表示新的interval。/** * Definition for an interval. * public class Interval { * int start; * int end; * In原创 2014-05-15 17:23:12 · 389 阅读 · 0 评论 -
leetcode:Reverse Words in a String
public class Solution { public static String reverseWords(String s) { String rst=""; int n=s.length(); int i,j; for(i=n-1;i>=0;i--) { if(s.charAt(i)!=' ') { j=i; while(j>=0 &&原创 2014-05-10 23:27:17 · 524 阅读 · 0 评论 -
leetcode: Jump Game
public class Solution { public boolean canJump(int[] A) { int i,j; int prev0=-1; int n=A.length; int max=0; for(i=0;i { if(A[i]==0)原创 2014-05-10 21:25:41 · 354 阅读 · 0 评论 -
leetcode: Spiral Matrix
考虑四种情况:设m为有几横行,n为有几竖列,第一种情况是m>n && n%2==1;第二种情况是m>n && n%2==0;第三种情况是m第四种情况是m分别算出每一中情况的终点[endx,endy]每次找一下一个点,只需要记录当前的方向(右,下,左,上),还有当前属于第几层,抵拢倒拐,碰到终点返回。public class Solution {原创 2014-05-08 21:31:57 · 472 阅读 · 0 评论 -
leetcode: Maximum Subarray
public class Solution { public int maxSubArray(int[] A) { int n=A.length; int[] F=new int[n]; int max,i; F[0]=A[0]; max=A[0]; for(i=1;i<n;i++) { if(A[i]>A[i]+F[i-1]) F[i原创 2014-05-08 21:45:48 · 379 阅读 · 0 评论 -
leetcode: N-Queens
public class Solution { public static ArrayList solveNQueens(int n) { int[] trace=new int[n]; int i,j,x; ArrayList rst=new ArrayList(); String[] B=null; char[] Bline=new char[n]; for原创 2014-05-08 19:00:56 · 381 阅读 · 0 评论 -
leetcode:Pow(x, n)
public class Solution { public static double pow(double x, int n) { if(x!=0 && n==0) return 1; if(x==0 && n>0) return 0; if(n==1 || x==1) return原创 2014-05-08 01:13:35 · 410 阅读 · 0 评论 -
leetcode:Anagrams
public class Solution { public static ArrayList anagrams(String[] strs) { int m=strs.length; int i,j,n; int[][] map=new int[m][26]; int[] Bhash=new int[m]; Arr原创 2014-05-07 19:05:41 · 409 阅读 · 0 评论 -
leetcode: First Missing Positive
public class Solution { public int firstMissingPositive(int[] A) { int i,j; int n=A.length; int e1,e2,tmp; for(i=0;i { while(i原创 2014-04-05 15:17:11 · 388 阅读 · 0 评论 -
leetcode: Swap Nodes in Pairs
交换需要三个指针,pre->p1->p2,其中p1,p2是要交换的两个元素。交换之后,pre->p2->p1;然后pre=p1,继续之后的交换。public class Solution { public ListNode swapPairs(ListNode head) { ListNode as_head=null; if(head==原创 2014-03-10 16:01:59 · 403 阅读 · 0 评论 -
leetcode: Merge k Sorted Lists
这道题借鉴了一下别人的写法。首先用堆是毫无疑问的,用堆找最值,可以讲时间复杂度将为LogN。java里的堆用PriorityQueue实现,实现的时候,要自己写Comparator接口的方法实现。因为leetcode只有一个方法,所以采用一种比较别扭的方法来写PriorityQueue的构造函数。取PriorityQueue的第一个最大元素,用poll()方法。注意判断链表节点为空原创 2014-03-10 15:31:33 · 487 阅读 · 0 评论 -
leetcode:Roman to Integer
注意优先看9**和4**public class Solution { public int romanToInt(String s) { int rst=0; int i,n; char[] ca=s.toCharArray(); n=s.length(); for(i=0;i原创 2014-02-28 10:33:48 · 523 阅读 · 0 评论 -
leetcode:【Max Points on a Line】
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.这道题考虑用如下表达式表达直线:point:(a0,b0), (a1,b1), line:(a1-a0)y+(b0-b1)x+(a0b1-a1b0)=0麻烦在于:相同点的原创 2014-02-18 16:26:25 · 327 阅读 · 0 评论 -
leetcode:Longest Common Prefix
还以为理解题意理解错了。。太简单了吧。。。就是对每个字符串的当前位比较。public class Solution { public String longestCommonPrefix(String[] strs) { int n=strs.length; if(n==0) return ""; Str原创 2014-02-28 15:20:05 · 404 阅读 · 0 评论 -
leetcode:Generate Parentheses
动归, s[n]=( s[j] )+s[n-j-1];这里注意一下ArrayList的用法,动态数组 ArrayList[] s=new ArrayList[n+1]; int i,j,k,m; for(i=0;i { s[i]=new ArrayList(); }原创 2014-02-28 10:14:49 · 378 阅读 · 0 评论 -
leetcode:Integer to Roman
代码略冗长。算法就是模拟,每次减一个5x,x是当前的位数,减到0为止。public class Solution { public String intToRoman(int num) { String rst=""; while(num>=1000) { rst+="M";原创 2014-02-24 17:01:16 · 382 阅读 · 0 评论 -
leetcode:Container With Most Water
这道题首先的思路是:设当前第i个直线为矮边界,找离i最远的比i高的边界,就构成以i为矮边界的容器。这是O(n^2)的算法,然后超时了。O(n^2)超时的话,要么分治,要么贪心。果然有贪心算法:从两边开始,将矮的一边往里推,直到i==i,记录每次的容器大小,是O(n)的算法。 int n=height.length; int h;原创 2014-02-24 16:36:20 · 433 阅读 · 0 评论 -
leetcode:Palindrome Number
不能用字符串,因为不能用额外空间,那么久一位一位的造出倒过来的数再比较一下。因为有越界问题,所以用long。public class Solution { public boolean isPalindrome(int x) { if(x return false; if(x return t原创 2014-02-23 15:51:22 · 365 阅读 · 0 评论 -
leetcode:Regular Expression Matching
这道题做了N久,主要是边界条件太麻烦了,老是有小问题,最后看了下别人的代码才改对。用递归:例如 aaaa和ab*a*a,不知道中间那个a*要吃掉多少个a,所以只能把所有的可能都尝试一遍。要判断正则表达式p=0;p=1; p>1&&p[1]=='*';p>1&&p[1]!='*'这四种情况。public class Solution { public boolean isMat原创 2014-02-24 09:47:24 · 636 阅读 · 0 评论 -
leetcode:String to Integer (atoi)
这道题做得很烦,一直没明白题意乱七八糟的。要用long来存储最后的结果,不然会越界。public class Solution {public int atoi(String str) { long rst; int n=str.length(); int i,begin,flag; int sn=1; rst=原创 2014-02-22 22:09:15 · 387 阅读 · 0 评论