array
文章平均质量分 64
yuanhisn
这个作者很懒,什么都没留下…
展开
-
LeetCode 152 - Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest pro...原创 2015-08-10 07:22:21 · 89 阅读 · 0 评论 -
LeetCode 18 - 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in ...原创 2015-05-06 03:52:09 · 44 阅读 · 0 评论 -
LeetCode 35 - Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. ...原创 2015-05-03 01:39:42 · 48 阅读 · 0 评论 -
Count the number of occurrences in a sorted array
Given a sorted array arr[] and a number x, write a function that counts the occurrences of x in arr[]. Expected time complexity is O(Logn) Examples: Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = ...原创 2015-05-01 05:56:36 · 75 阅读 · 0 评论 -
Google Interview - Wiggle Sort
Write a function to convert the array into alternate increasing decreasing numbers: a[0] <= a[1] >= a[2] <= a[3] >=... Note: You should solve it in place and one pass. Solution 1: ...原创 2015-04-27 11:43:41 · 53 阅读 · 0 评论 -
Counting Sort
Let us understand it with the help of an example. For simplicity, consider the data in the range 0 to 9. Input data: 1, 4, 1, 2, 7, 5, 2 1) Take a count array to store the count of each unique ...原创 2015-03-29 13:33:35 · 78 阅读 · 0 评论 -
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 3rd largest element is 4 In array [1,2,3,4,5], the 1st largest element ...原创 2015-03-27 04:12:51 · 78 阅读 · 0 评论 -
两个有序数组的交集和并集
两个有序数组的交集和并集。 Intersection of two sorted array. (find the common elements between two sorted arrays) Follow up: 找出两个有序数组里不同的数字(类似求集合的异或) 这是Facebook的电面题。 /** * 求解两个有序数组的交集 * @param a...原创 2015-03-27 00:29:38 · 168 阅读 · 0 评论 -
LeetCode 88 - Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. ...原创 2015-03-26 12:06:07 · 52 阅读 · 0 评论 -
Google&Facebook Interview - Find subarray with given sum
Given an unsorted array of nonnegative integers, find a continous subarray which adds to a given number. Examples: Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33 Ouptut: Sum found between indexes 2...原创 2015-05-18 04:03:27 · 54 阅读 · 0 评论 -
Sort Part to Make Entire Array Sorted
Given an unsorted array arr[0..n-1] of size n, find the minimum length subarray arr[s..e] such that sorting this subarray makes the whole array sorted. Examples:1) If the input array is [10, 12, ...原创 2015-07-30 00:44:53 · 70 阅读 · 0 评论 -
Airbnb Interview - Nested Integer List Parser
实现一个mini parser, 输入是以下格式的string:"324" or"[123,456,[788,799,833],[[]],10,[]]"要求输出:324 or [123,456,[788,799,833],[[]],10,[]].也就是将字符串转换成对应的格式的数据. 输入一个数组的字符串, 要返回一个数组, 里面每一个元素是要么一个整数, 要么是一个数组. 但是注意数组可以...原创 2015-07-26 13:55:05 · 52 阅读 · 0 评论 -
Google Interview - Second Largest Number
Find the second largest number in a given array.Return 0 if the given array has no second largest number. public int secondLargest(int[] arr) { if(arr.length<2) return 0; int first =...原创 2015-06-15 13:19:00 · 68 阅读 · 0 评论 -
LeetCode 1 - Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, wher...原创 2015-06-09 04:06:55 · 43 阅读 · 0 评论 -
LintCode - Subarray Sum Zero
Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number. Example Given [-3, 1, 2, -3, 4]...原创 2015-06-04 11:01:39 · 49 阅读 · 0 评论 -
LintCode - Minimum Size Subarray Sum
Given 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. Example Given the array [2,3,1,...原创 2015-06-04 10:35:16 · 58 阅读 · 0 评论 -
LeetCode 169 - Majority Number
Majority Number I Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it. Example Given [1, 1, 1, 1, 2, 2, 2], return 1 ...原创 2015-06-02 13:34:55 · 68 阅读 · 0 评论 -
Equilibrium index of an array
Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an arrya A: A[0] = -7, A[1] = 1, A[2] = 5,...原创 2015-05-31 13:40:36 · 55 阅读 · 0 评论 -
Counting inversions in an array
Count the number of inversion pairs in an array of n numbers. An inversion pair is defined as following: - If i < j and arr[i] > arr[j] then (arr[i], arr[j]) is called inve...原创 2015-03-24 11:00:19 · 101 阅读 · 0 评论 -
LeetCode 84 - Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of ea...原创 2015-03-24 10:00:52 · 39 阅读 · 0 评论 -
LeetCode 153 - Find Minimum in Rotated Sorted 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 the ar...原创 2015-02-21 11:21:45 · 53 阅读 · 0 评论 -
LeetCode 11 - Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two...原创 2015-02-16 05:07:59 · 47 阅读 · 0 评论 -
Find two non-repeating elements in an array
一个int型数组里面所有数都出现两次,只有两个数出现一次。找出这两个数。 Solution: public void findTwoUniqueNumbers(int[] A) { int x = 0; for(int i=0; i<A.length; i++) { x ^= A[i]; } int lsb = x & -x; int a = 0, ...原创 2015-02-14 11:52:32 · 62 阅读 · 0 评论 -
LeetCode - Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in...原创 2015-01-13 03:48:17 · 67 阅读 · 0 评论 -
LeetCode - Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the a...原创 2015-01-10 09:42:36 · 53 阅读 · 0 评论 -
LeetCode 54 - Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] ...原创 2015-02-21 14:16:00 · 45 阅读 · 0 评论 -
LeetCode 162 - 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, in th...原创 2015-03-21 01:20:23 · 73 阅读 · 0 评论 -
Facebook interview - Move all zeroes to end of array
Given an array of random numbers, Push all the zero’s of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, ...原创 2015-03-19 03:29:40 · 59 阅读 · 0 评论 -
LeetCode 119 - Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? public List<Integ...原创 2015-03-13 00:18:32 · 47 阅读 · 0 评论 -
LeetCode 118 - Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] public List<List<I...原创 2015-03-13 00:18:15 · 40 阅读 · 0 评论 -
LeetCode 163 - Missing Ranges
Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing ranges.For example, given [0, 1, 3, 50, 75], return [“2”, “4->49”, “51->74”, “76->99”] 这也...原创 2015-03-09 10:44:54 · 83 阅读 · 0 评论 -
K Sum
2sum的算法复杂度是O(N log N) 因为排序用了N log N以及头尾指针的搜索是线性的,所以总体是O(N log N),好了现在考虑3sum, 有了2sum其实3sum就不难了,这样想:先取出一个数,那么我只要在剩下的数字里面找到两个数字使得他们的和等于(target – 那个取出的数)就可以了吧。所以3sum就退化成了2sum, 取出一个数字,这样的数字有N个,所以3sum的算法复...原创 2015-03-05 03:45:00 · 51 阅读 · 0 评论 -
LeetCode 128 - Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]...原创 2015-02-25 02:58:21 · 42 阅读 · 0 评论