JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

2013年11月5日

摘要: use stack 1 public class Solution { 2 public String addBinary(String a, String b) { 3 if(a == null||b == null) 4 return ""; 5 if(a.length() == 0) 6 return b; 7 if(b.length() == 0) 8 return a; 9 Stack stack = new Stack();10 ... 阅读全文
posted @ 2013-11-05 17:09 JasonChang 阅读(141) 评论(0) 推荐(0) 编辑

摘要: use stack 1 public class Solution { 2 public int[] plusOne(int[] digits) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(digits == null) 6 return null; 7 Stack myst... 阅读全文
posted @ 2013-11-05 16:56 JasonChang 阅读(187) 评论(0) 推荐(0) 编辑

摘要: recursion programming 1 public class Solution { 2 public int uniquePaths(int m, int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int[][] result = new int[m+1][n+1]; 6 result... 阅读全文
posted @ 2013-11-05 16:24 JasonChang 阅读(163) 评论(0) 推荐(0) 编辑

摘要: 1 public class Solution { 2 public int lengthOfLastWord(String s) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(s == null) 6 return 0; 7 char[] mychar = s.trim()... 阅读全文
posted @ 2013-11-05 16:23 JasonChang 阅读(174) 评论(0) 推荐(0) 编辑

摘要: recursion programming 1 public class Solution { 2 public String countAndSay(int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(n == 1) 6 return "1"; 7 char[] my... 阅读全文
posted @ 2013-11-05 16:13 JasonChang 阅读(210) 评论(0) 推荐(0) 编辑

摘要: use three 2-d matrix to store if any column, row or block has more than one specific value. 1 public class Solution { 2 public boolean isValidSudoku(char[][] board) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for ... 阅读全文
posted @ 2013-11-05 16:02 JasonChang 阅读(158) 评论(0) 推荐(0) 编辑

摘要: simple recursion programming problemelse if(target < A[mid]) return binarySearch(A, start, mid, target); else return binarySearch(A, mid+1, end, target); 1 public class Solution { 2 public int searchInsert(int[] A, int target) { 3 // IMPORTANT: Please res... 阅读全文
posted @ 2013-11-05 15:47 JasonChang 阅读(222) 评论(0) 推荐(0) 编辑

摘要: same asRemove Duplicates from Sorted Array 1 public class Solution { 2 public int removeElement(int[] A, int elem) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(A == null||A.length <... 阅读全文
posted @ 2013-11-05 15:35 JasonChang 阅读(167) 评论(0) 推荐(0) 编辑

摘要: use two index to compress array 1 public class Solution { 2 public int removeDuplicates(int[] A) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(A == null||A.length < 1) 6 ... 阅读全文
posted @ 2013-11-05 15:19 JasonChang 阅读(204) 评论(0) 推荐(0) 编辑

摘要: recursion programming 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * }10 * }11 */12 public class Solution {13 public ListNode swapPairs(List... 阅读全文
posted @ 2013-11-05 14:56 JasonChang 阅读(160) 评论(0) 推荐(0) 编辑

摘要: fastrunner and slowrunner trick.(be care of head node) 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * }10 * }11 */12 public class Solution {13 ... 阅读全文
posted @ 2013-11-05 14:37 JasonChang 阅读(153) 评论(0) 推荐(0) 编辑

摘要: recursion problem 1 public class Solution { 2 public ArrayList letterCombinations(String digits) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList result = new ArrayList(); 6 ... 阅读全文
posted @ 2013-11-05 14:07 JasonChang 阅读(205) 评论(0) 推荐(0) 编辑

摘要: similar with 3Sum 1 public class Solution { 2 public int threeSumClosest(int[] num, int target) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(num == null || num.length < 3) 6 ... 阅读全文
posted @ 2013-11-05 13:47 JasonChang 阅读(141) 评论(0) 推荐(0) 编辑

摘要: 1 public class Solution { 2 public ArrayList generateParenthesis(int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList result = new ArrayList(); 6 StringBuilder builde... 阅读全文
posted @ 2013-11-05 12:19 JasonChang 阅读(192) 评论(0) 推荐(0) 编辑

摘要: 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * }10 * }11 */12 public class Solution {13 public ListNode mergeTwoLists(ListNode l1, ListNode... 阅读全文
posted @ 2013-11-05 12:05 JasonChang 阅读(154) 评论(0) 推荐(0) 编辑

摘要: use stack (be careful of empty stack) 1 public class Solution { 2 public boolean isValid(String s) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(s == null||s.length() == 0) 6 ... 阅读全文
posted @ 2013-11-05 11:54 JasonChang 阅读(169) 评论(0) 推荐(0) 编辑

摘要: be careful of the special cases(null, 0) 1 public class Solution { 2 public String longestCommonPrefix(String[] strs) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(strs == null||strs... 阅读全文
posted @ 2013-11-05 11:39 JasonChang 阅读(186) 评论(0) 推荐(0) 编辑

摘要: 当前一位小于后一位的时候才是后一位减去前一位,并且这种位数只有一位 1 public class Solution { 2 public int romanToInt(String s) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int result = 0; 6 if (s == null || s.... 阅读全文
posted @ 2013-11-05 09:38 JasonChang 阅读(160) 评论(0) 推荐(0) 编辑

摘要: use stringbuffer 1 public class Solution { 2 public String intToRoman(int num) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 StringBuffer result = new StringBuffer(); 6 String[]... 阅读全文
posted @ 2013-11-05 09:18 JasonChang 阅读(139) 评论(0) 推荐(0) 编辑

摘要: brute force O(n2)从头尾两头扫面 O(n) 1 public class Solution { 2 public int maxArea(int[] height) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 int start = 0; 6 int end = height.length... 阅读全文
posted @ 2013-11-05 09:12 JasonChang 阅读(168) 评论(0) 推荐(0) 编辑