- 博客(40)
- 收藏
- 关注
原创 Wildcard Matching
public class Solution { public boolean isMatch(String s, String p) { int i = 0, j = 0; // p.charAt(j) == '*', sAsterisk = i, pAsterisk = ++j int sAsterisk = 0, pAsterisk =
2014-07-08 06:13:19
254
原创 Regular Expression Matching
public class Solution { public boolean isMatch(String s, String p) { if(p.length() == 0) { return s.length() == 0; } if(p.length() == 1) { return s.
2014-07-08 05:28:01
223
原创 Longest Palindromic Substring
public class Solution { public String longestPalindrome(String s) { if(s.length() < 2) { return s; } // DP // P[i][j] == true // s.substring(i,
2014-07-08 02:43:11
229
原创 Add Binary
public class Solution { public String addBinary(String a, String b) { StringBuffer sum = new StringBuffer(); int carry = 0; for(int i = a.length() - 1, j = b.length() - 1;
2014-07-08 02:37:16
265
原创 String to Integer (atoi)
public class Solution { public int atoi(String str) { str = str.trim(); if(str.matches("(\\+?|-?)\\d+.*")) { boolean isPos = true; if(str.charAt(0) == '-')
2014-07-08 02:12:59
205
原创 Implement strStr()
public class Solution { public String strStr(String haystack, String needle) { if(needle == "") { return haystack; } for(int i = 0; i <= haystack.length() - nee
2014-07-08 02:02:25
187
原创 Valid Palindrome
public class Solution { public boolean isPalindrome(String s) { s = s.trim().replaceAll("\\W", "").toLowerCase(); for(int i = 0; i < (s.length() + 1) / 2; i++) { if(s.c
2014-07-08 01:55:02
198
原创 Copy List with Random Pointer
/** * Definition for singly-linked list with a random pointer. * class RandomListNode { * int label; * RandomListNode next, random; * RandomListNode(int x) { this.label = x; } * };
2014-07-07 23:30:31
244
原创 Reverse Nodes in k-Group
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public
2014-07-07 12:13:03
228
原创 Swap Nodes in Pairs
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public
2014-07-07 08:45:38
182
原创 Remove Nth Node From End of List
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public
2014-07-07 08:32:04
253
原创 Rotate List
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public
2014-07-07 08:28:45
189
原创 Remove Duplicates from Sorted List II
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public
2014-07-07 08:19:46
283
原创 Remove Duplicates from Sorted List
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public
2014-07-07 06:43:56
192
原创 Partition List
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public
2014-07-07 06:34:43
185
原创 Add Two Numbers
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public
2014-07-07 06:25:37
227
原创 Reverse Linked List II
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public
2014-07-07 06:22:50
178
原创 Single Number II
public class Solution { public int singleNumber(int[] A) { // O(1) Space // Bit operation for ternery int once = 0, twice = 0, thrice = 0; // Indicates how many times '
2014-07-06 13:46:42
210
原创 Single Number
public class Solution { public int singleNumber(int[] A) { // O(1) Space // Exclusive or int xor = A[0]; for(int i = 1; i < A.length; i++) { xor ^= A[i]
2014-07-06 13:28:42
200
原创 Candy
public class Solution { public int candy(int[] ratings) { int[] increments = new int[ratings.length]; // From left to right for(int i = 1, increment = 1; i < ratings.length
2014-07-06 12:57:07
196
原创 Gas Station
public class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int posSum = 0, circuitSum = 0; int index = -1; for(int i = 0; i < gas.length; i++) {
2014-07-06 12:41:00
211
原创 Set Matrix Zeroes
public class Solution { public void setZeroes(int[][] matrix) { // O(1) space solution // The first row and the first column boolean firstRowHasZero = false, firstColHasZer
2014-07-06 10:43:22
356
原创 Gray Code
public class Solution { public List grayCode(int n) { // Reflect-and-prefix // nth: n xor n/2 List gray = new ArrayList(); if(n == 0) { gray.add(0);
2014-07-06 09:20:09
246
原创 Climbing Stairs
public class Solution { public int climbStairs(int n) { // Fibonacci // climbStairs(n) = climbStairs(n - 1) + climbStairs(n - 2) int prev = 1, curr = 1; for(int i =
2014-07-06 05:46:59
181
原创 Plus One
public class Solution { public int[] plusOne(int[] digits) { boolean hasCarry = false; for(int i = digits.length - 1; i >= 0; i--) { digits[i]++; if(digits[
2014-07-06 05:35:30
274
原创 Rotate Image
public class Solution { public void rotate(int[][] matrix) { recRotate(matrix, matrix.length); } private void recRotate(int[][] matrix, int n) { if(n < 2) {
2014-07-06 05:11:45
206
原创 Trapping Rain Water
public class Solution { public int trap(int[] A) { if(A == null || A.length < 2) { return 0; } int area = 0; int maxInd = findMaxInd(A); int lef
2014-07-06 04:59:25
179
原创 Valid Sudoku
public class Solution { public boolean isValidSudoku(char[][] board) { // 9 * 9 board // Row for(int i = 0; i < 9; i++) { boolean[] used = new boolean[9];
2014-07-06 04:51:10
172
原创 Permutation Sequence
public class Solution { public String getPermutation(int n, int k) { int[] A = new int[n]; for(int i = 0; i < n; i++) { A[i] = i + 1; } return kthPermut
2014-07-06 02:04:04
236
原创 Next Permutation
public class Solution { public void nextPermutation(int[] num) { if(num.length < 2) { return; } // Finds the start of descending sequence (consisting of last fe
2014-07-05 07:23:26
189
原创 Remove Element
public class Solution { public int removeElement(int[] A, int elem) { int index = 0; for(int i = 0; i < A.length; i++) { if(A[i] != elem) { A[index++] =
2014-07-05 03:47:07
215
原创 4Sum
public class Solution { public List> fourSum(int[] num, int target) { HashSet> quadruplets = new HashSet>(); if(num.length < 4) { return new ArrayList>(quadruplets);
2014-07-05 03:38:44
212
原创 3Sum Closest
public class Solution { public int threeSumClosest(int[] num, int target) { Arrays.sort(num); // Two pointers scan int closest = num[0] + num[1] + num[2]; for(int i
2014-07-05 03:29:00
205
原创 3Sum
public class Solution { public ArrayList> threeSum(int[] num) { HashSet> triplets = new HashSet>(); Arrays.sort(num); // Two pointers scan for(int i = 0; i < num.le
2014-07-05 03:21:30
273
原创 Longest Consecutive Sequence
public class Solution { public int longestConsecutive(int[] num) { HashSet set = new HashSet(); // Initializes the set for(int i = 0; i < num.length; i++) { set
2014-07-05 03:03:21
189
原创 Median of Two Sorted Arrays
public class Solution { public double findMedianSortedArrays(int A[], int B[]) { int sumOfLen = A.length + B.length; return sumOfLen % 2 == 1 ? recFindK(A, B, 0, A.length
2014-07-04 11:19:25
244
原创 Search in Rotated Sorted Array II
public class Solution { public boolean search(int[] A, int target) { // Iterative binary search int left = 0, right = A.length - 1; while(left <= right) {
2014-07-04 03:34:43
190
原创 Search in Rotated Sorted Array
public class Solution { public int search(int[] A, int target) { // Iterative binary search int left = 0, right = A.length - 1; while(left <= right) { int mid =
2014-07-04 03:17:43
186
原创 Remove Duplicates from Sorted Array II
public class Solution { public int removeDuplicates(int[] A) { if(A.length < 2) { return A.length; } // Scans the array in linear time int i
2014-07-04 02:24:33
197
原创 Remove Duplicates from Sorted Array
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode deleteDup
2014-07-04 01:11:15
191
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人