![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
文章平均质量分 62
baidu_17223201
这个作者很懒,什么都没留下…
展开
-
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 · 245 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论 -
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 阅读 · 0 评论