
数组
文章平均质量分 68
nicaishibiantai
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Minimum size subarray sum
iven an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,原创 2015-05-18 06:15:08 · 995 阅读 · 0 评论 -
Lintcode - Maximum Subarray II
Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Note The subarray shoul原创 2015-02-08 15:12:28 · 4198 阅读 · 0 评论 -
Missing Ranges
Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->原创 2014-12-15 08:19:32 · 554 阅读 · 0 评论 -
Fraction to recurring decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses.原创 2014-12-30 07:20:07 · 483 阅读 · 0 评论 -
Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element原创 2014-12-28 09:36:17 · 367 阅读 · 0 评论 -
Distinct Subsequences
public int numDistinct(String S, String T) { int row = S.length(); int col = T.length(); int[][] m = new int[row][col]; for (int i = 0; i < row; i++) {原创 2014-09-02 06:52:38 · 376 阅读 · 0 评论 -
各种3sum 4sum
这两天做了3sum, 3sum closest, 4sum 一个模板就是先sort array, 取一个或两个下标(depend on 3sum or 4sum) 然后用另外两个指针指向头和尾。由于数组已经排序,只需要这两个指针扫一遍。 教训:当sum == target时,经常忘记移动两个下标(k, l),使得死循环或者不再去判断k+1到l-1之间的数组。 public class So原创 2014-08-11 11:30:38 · 481 阅读 · 0 评论 -
First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant原创 2014-09-05 12:52:54 · 469 阅读 · 0 评论 -
lintcode - kth prime number
Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7. The eligible numbers are like 3, 5, 7, 9, 15 ... Example If k=4, return 9. Challenge O(n原创 2015-02-01 07:32:09 · 1632 阅读 · 1 评论 -
Lintcode - Product of Array Exclude Itself
Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B without divide operation. Example For A=[1, 2, 3], B is [6, 3, 2] public ArrayList produ原创 2015-01-30 13:14:53 · 3252 阅读 · 1 评论 -
Lintcode - Search in 2D matrix II
Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of it. This matrix has the following properties: * Integers in each row are sorted from left原创 2015-01-30 14:50:06 · 1880 阅读 · 0 评论 -
Find Minimum in Rotated Sorted Array II
public int findMin(int[] num) { int left = 0; int right = num.length-1; while (left = num[right]) { int mid = left + (right-left)/2; if (num[mid] == nu原创 2014-11-24 08:59:11 · 380 阅读 · 0 评论 -
Find Minimum Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in原创 2014-11-24 08:29:23 · 503 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,原创 2014-10-13 06:55:16 · 340 阅读 · 0 评论 -
Lintcode - Majority Number II
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Find it. Note There is only one majority number in the array Example原创 2015-02-08 09:04:09 · 1989 阅读 · 0 评论 -
Lintcode - Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1/k of the size of the array. Find it. Note There is only one majority number in the array.原创 2015-02-08 14:32:07 · 4133 阅读 · 1 评论 -
Lintcode - Heapify
Given an integer array, heapify it into a min-heap array. For a heap array A, A[0] is the root of heap, and for each A[i], A[i * 2 + 1] is the left child of A[i] and A[i * 2 + 2] is the right child原创 2015-02-08 08:34:14 · 2330 阅读 · 0 评论 -
Reservoir sampling
Having an input stream of n, select k elements with equal probability. Algorithm: 1. select k elements into the result first 0....k-1 2. for k....n-1 elements, each time i, generate a random n原创 2015-04-17 11:58:43 · 692 阅读 · 0 评论 -
Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, i原创 2015-04-08 13:28:53 · 476 阅读 · 0 评论 -
Lintcode - kth largest element
Find K-th largest element in an array. Note You can swap elements in the array Example In array [9,3,2,4,8], the 3th largest element is 4 Challenge O(n) time, O(1) space原创 2015-01-30 12:07:30 · 1909 阅读 · 0 评论 -
Lintcode - Maximum Subarray III
Given an array of integers and a number k, find knon-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Note The su原创 2015-03-24 05:21:00 · 4434 阅读 · 1 评论 -
Remove element
Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 简单题原创 2015-03-24 02:29:48 · 429 阅读 · 0 评论 -
Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4] public void rotate(int[] nums, int k) {原创 2015-03-30 05:20:19 · 346 阅读 · 0 评论 -
Multiply String
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. public String multiply(String n原创 2014-09-01 12:52:29 · 603 阅读 · 0 评论 -
Lintcode - Backpack II
Given n items with size A[i] and value V[i], and a backpack with size m. What's the maximum value can you put into the backpack? Note You cannot divide item into small pieces and the total siz原创 2015-03-17 13:13:10 · 2394 阅读 · 0 评论 -
Lintcode - Maximum Subarray Difference
Given an array with integers. Find two non-overlapping subarrays A and B, which|SUM(A) - SUM(B)| is the largest. Return the largest difference. Note The subarray should contain at least on原创 2015-03-20 13:02:48 · 4705 阅读 · 0 评论 -
Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements原创 2015-03-26 13:21:51 · 438 阅读 · 0 评论 -
Lintcode - Previous Permutation
Given a list of integers, which denote a permutation. Find the previous permutation in ascending order. Note The list may contains duplicate integers. Example For [1,3,2,3], the pre原创 2015-02-13 15:21:52 · 4906 阅读 · 0 评论 -
Read N Characters Given Read4 II - Call multiple times
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the原创 2015-01-22 14:28:44 · 628 阅读 · 0 评论 -
Lintcode - k sum II
Given n unique integers, number k (1 Example Given [1,2,3,4], k=2, target=5, [1,4] and [2,3] are possible solutions. public ArrayList> kSumII(int A[], int k, int target) { Array原创 2015-02-09 14:42:57 · 1875 阅读 · 0 评论 -
Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with原创 2014-10-13 06:35:34 · 348 阅读 · 0 评论 -
Next Permutation
public void nextPermutation(int[] num) { int n = num.length; if (n <= 1) { return; } int cur = n-1; while (cur >= 1 && num[cur-1]>= num[cur]) {原创 2014-08-31 13:52:04 · 446 阅读 · 0 评论 -
Permutation Sequence
public String getPermutation(int n, int k) { List num = new ArrayList(); for (int i = 0; i < n; i++) { num.add(i+1); } int factorial = 1; for (int i原创 2014-09-21 04:03:22 · 655 阅读 · 0 评论 -
Max Points on a Line
public int maxPoints(Point[] points) { int max = 1; if (points.length == 0) { return 0; } for (int i = 0; i < points.length-1; i++) { Point curP原创 2014-09-21 07:00:03 · 439 阅读 · 0 评论 -
surrounded region
http://leetcode.com/oldoj#question_130 Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region .原创 2014-08-11 11:31:18 · 373 阅读 · 0 评论 -
Find prime numbers
public static List findPrime(int n) { List preResult = new ArrayList(); List result = new ArrayList(); for (int i = 1; i <= n; i++) { preResult.add(i); } int divisorInd = 1; // start fro原创 2014-08-25 11:57:42 · 526 阅读 · 0 评论 -
Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least on原创 2014-09-08 12:25:23 · 355 阅读 · 0 评论 -
GrayCode
The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gr原创 2014-08-11 11:31:46 · 682 阅读 · 0 评论 -
旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 犯错的地方:开始忘了i是从offset开始,以为从0开始 public class Solution {原创 2014-08-11 11:31:59 · 355 阅读 · 0 评论 -
sudoku
1. valid sudoku 就是检测每行,每列,每个block是否有重复数字。 public class Solution { public boolean isValidSudoku(char[][] board) { // Start typing your Java solution below // DO NOT write main() fu原创 2014-08-11 11:31:09 · 443 阅读 · 0 评论