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