数组/集合 (array / set)
文章平均质量分 65
beiyetengqing
http://blog.csdn.net/beiyeqingteng 的镜像站
展开
-
求两个排序数组的并集
问题: 给你两个排序的数组,求两个数组的并集。比如: A = 1 3 4 5 7, B = 2 3 5 8 9, 那么并集就是 1 2 3 4 5 7 8 9.代码:public class Union { public static void main(String[] args) { char[] ch1 = "abcde".toCharArray(); c原创 2012-07-23 06:37:53 · 4683 阅读 · 0 评论 -
Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] ha原创 2013-01-05 13:16:03 · 576 阅读 · 0 评论 -
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:原创 2013-01-05 08:36:26 · 1193 阅读 · 0 评论 -
array shuffling
Suppose we have an array a1, a2, ..., an, b1, b2, ..., bn. Implement an algorithm to change this array to a1, b1, a2, b2, ..., an, bn.public void shuffle(char[] arr) { if (arr == null || arr.length原创 2013-02-02 02:57:13 · 864 阅读 · 0 评论 -
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,原创 2013-01-18 23:37:38 · 628 阅读 · 0 评论 -
Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,原创 2013-01-19 01:59:42 · 1289 阅读 · 0 评论 -
随机Set
问题:How would you define a data structure that stores a set ofvalues (i.e., a value cannot appear more than one time), and implements the following functions:add(p)--adds the value p to the setde转载 2013-02-05 07:38:34 · 765 阅读 · 0 评论 -
Best Time to Buy and Sell Stock II
Question:Say you have an array for which the i-th element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you li原创 2012-12-07 07:25:02 · 682 阅读 · 0 评论 -
给你一串股票价格,找出买点和卖点,使得利润最大
问题:Given array of integers representing historic stock prices. Find the buying and selling points to maximize the profit. 给你一个整数数组,数组的值代表股票的历史价格。找到买点和卖点,使得利润最大。假设数组为array = {3, 8, 1, 2, 19, 22, 2,原创 2012-07-23 10:38:53 · 2137 阅读 · 0 评论 -
Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note:You ma原创 2013-01-21 05:38:19 · 834 阅读 · 0 评论 -
在数组里查找这样的数,它大于等于左侧所有数,小于等于右侧所有数
问题:一个int数组, 比如 array[],里面数据无任何限制,要求求出所有这样的数array[i],其左边的数都小于等于它,右边的数都大于等于它。能否只用一个额外数组和少量其它空间实现。分析:最原始的方法是检查每一个数 array[i] ,看是否左边的数都小于等于它,右边的数都大于等于它。这样做的话,要找出所有这样的数,时间复杂度为O(N^2)。其实可以有更简单的方法,我们原创 2013-02-11 11:59:22 · 2482 阅读 · 0 评论 -
数组中找出第k大的值
最简单的办法,就是先排序,然后把第K个值找出来,这样算法的复杂度为 O(nlgn).其实还有一种更简单的方法,其平均复杂度 为 O(lgn),当然,最坏是 O(n^2). 这种算法就是利用了 quicksort 和二分查找 的做法。先把数组分成两个部分,左边一个部分比pivot小,另一边比pivot大。然后再观察pivot的位置,如果pivot的位置比 K大,那么那个第K个值就一定在pivot原创 2013-03-13 23:48:31 · 1227 阅读 · 0 评论 -
Median of Two Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log(m+n)).O(m + n) solution: public stat原创 2013-01-30 11:50:10 · 1696 阅读 · 0 评论 -
Search 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).You are given a target value to search. If found in the array ret原创 2013-01-03 23:09:33 · 2598 阅读 · 0 评论 -
Plus One
Given a number represented as an array of digits, plus one to the number.public class Solution { public int[] plusOne(int[] digits) { if (digits == null || digits.length == 0) return nul原创 2012-12-30 13:30:23 · 783 阅读 · 0 评论 -
求两个数组的交集
问题: 给你两个排序的数组,求两个数组的交集。 比如: A = 1 3 4 5 7, B = 2 3 5 8 9, 那么交集就是 3 5.分析:因为数组A B均排过序,所以,我们可以用两个“指针”分别指向两个数组的头部,如果其中一个比另一个小,移动小的那个数组的指针;如果相等,那么那个值是在交集里,保存该值,这时,同时移动两个数组的指针。一直这样操作下去,直到有一个指针已经超过原创 2012-07-23 06:12:51 · 916 阅读 · 0 评论 -
数组中出现次数超过一半的数字
问题:给定一个 int 数组,找出里面出现次数超过一半的数字。分析:首先,我们假定该数组中存在超过一半的数字,要把那个数字找出来,我们有以下方法:1. 排序。出现次数最大的那个数字一定是排序后第 n/2 + 1 那个值(假定是从1 开始数)。复杂度为O(nlgn).2. 遍历一遍数组,找出数组里的最大值max和最小值min,然后创建一个大小为 max - min + 1的数组,原创 2012-08-21 04:54:56 · 751 阅读 · 0 评论 -
Merge Sorted Array
Question: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 to hold additional elements from B. The number of elements init原创 2012-12-08 02:19:52 · 1432 阅读 · 0 评论 -
Remove Element
Question: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 n原创 2012-12-09 06:47:49 · 3029 阅读 · 0 评论 -
Remove Duplicates from Sorted Array
Question: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 thi原创 2012-08-29 06:59:16 · 2050 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II
Question: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原创 2012-12-09 07:05:25 · 3189 阅读 · 0 评论 -
Unique Paths II
Question:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respect原创 2012-12-10 03:55:09 · 1800 阅读 · 0 评论 -
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.原创 2012-12-12 05:09:28 · 1838 阅读 · 1 评论 -
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原创 2012-12-13 07:03:28 · 552 阅读 · 0 评论 -
Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0原创 2012-12-27 10:43:06 · 607 阅读 · 0 评论 -
Sliding Window Maximum
A long array A[] is given to you. There is a sliding window of size w which is moving from the very left of the array to the very right. You can only see the w numbers in the window. Each time the sli转载 2012-12-27 00:18:54 · 764 阅读 · 0 评论 -
First Missing Positive
Question: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原创 2012-12-28 06:52:58 · 2212 阅读 · 0 评论 -
Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to原创 2013-10-13 11:53:16 · 849 阅读 · 0 评论