数组问题
ojshilu
https://github.com/lucky521
展开
-
数组中每个数会出现3次,只有1个例外的数仅出现1次。 Single Number
题目源自于leetcode。好题。题目:Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you im原创 2013-11-09 22:33:14 · 7240 阅读 · 0 评论 -
Triangle Path 寻找最小和路径
问题:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4],原创 2014-02-26 20:14:01 · 1048 阅读 · 0 评论 -
全排列生成 Permutations
题目源自于leetcode。题目:Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].原创 2013-11-13 18:44:50 · 1512 阅读 · 0 评论 -
插入时间段 Insert Interval
题目:Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.原创 2014-02-20 21:57:43 · 1105 阅读 · 0 评论 -
寻找两个不等长数组的中位数 Median of Two Sorted Arrays
题目源自于Leetcode。经典好题。题目:给出两个有序数组,长度不一定相同,一个是m一个是n,要求给出他们合并在一起之后的数组的中位数。要求时间复杂度为O(log(m+n)),所以不可以合并数组再找中位数,否则复杂度就是O(m+n)。数组的中位数如果有序数组有奇数个元素,那么中位数是最中间那个元素。如果有序数组有偶数个元素,那么中位数是中间两个元素的平均数。原创 2013-11-10 14:42:02 · 4886 阅读 · 1 评论 -
荷兰国旗问题 将3种数中重复数聚集 Sort Colors
题目源自于leetcode。好题。普通方法很好想,高效方法想半天。题目:有一个数组,元素只有0,1,2这三种,现在要将其排序。0全放前面,2全放后面,1放中间。要求:只遍历一趟。空间复杂度为O(1)。思路: 换个角度,海阔天空。起初我一直想用左右端指针做,把左端的非0换成0,把右端的非2换成2。但是到中间部位就乱了,因为左右端是同时进行的。 看了网上的解答。方法非常原创 2013-11-28 21:22:44 · 1518 阅读 · 0 评论 -
寻找最大储水量的容器 Container With Most Water
题目源自于leetcode。题目:数组的n个数值代表n个高度各异的垂线段。以x轴作为底座,选2个垂线段作为高。形成一个三面的容器。求储水量最大的那个。方法:容器的储水量与底座长度和2边高的较小值有关。如果使用双循环依次选取两个高进行判断,时间复杂度太高。O(n*n)。较好的方法是左右端双指针向中间接近。因为要找的是最大值,所以让底座首先是最大值。如果左指针所在的高度较低,则原创 2013-10-17 08:55:28 · 1918 阅读 · 0 评论 -
数组的最大子数组和 Maximum Subarray
题目源自于Leetcode。题目: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 subar原创 2013-11-08 15:05:52 · 1296 阅读 · 0 评论 -
数组的最大子数组积 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 large原创 2014-10-20 10:40:24 · 1221 阅读 · 0 评论 -
股票最大收益问题3 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.给出一个数组,这一次,要求的原创 2014-02-28 21:49:36 · 1721 阅读 · 0 评论 -
股票最大收益 Best Time to Buy and Sell Stock II
题目源自于leetcode。题目: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 as many transactions as you原创 2013-11-06 19:39:45 · 1248 阅读 · 0 评论 -
寻找序列的插入位置 Search Insert Position
题目源自于leetcode。题目: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 duplica原创 2013-11-06 20:00:02 · 1389 阅读 · 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原创 2014-03-01 21:20:04 · 1058 阅读 · 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 l原创 2014-03-16 20:03:45 · 1180 阅读 · 0 评论 -
原地归并数组 Merge Sorted Array
题目源自于leetcode。题目:将有序数组B归并到有序数组A中,已知A中的剩余空间足够。思路:1、尽量不用额外空间来进行归并。2、尽可能减少移动元素的次数,最好能一次性移到最终位置上。方法:从尾部开始归并。原地进行。新数组的尾部游标是A、B数组元素之和,因此归并过程中新数组的尾部游标不会超过A数组的尾部游标。代码:class Solution {public:原创 2014-02-17 16:27:37 · 1159 阅读 · 0 评论 -
寻找不在数组中最小的正整数 First Missing Positive
问题:给出一个无序的数组,其中包含有任意的整数。现在要求返回不包含在数组中的最小的正整数。要求:时间复杂度O(n),空间复杂度O(1)。思路:如果不要求空间复杂度,可以用hash、map进行统计。但是现在,不允许借助临时空间。但是对于无序数组,不借助空间怎么可能统计的出来。因此,唯一的出路,就是把输入数组自己作为临时空间使用。这道题用了一个类似计数排序的技巧。把一个正数是否存在这一原创 2014-03-08 20:14:49 · 3013 阅读 · 0 评论 -
杨氏矩阵查找 Search a 2D Matrix
题目源自于leetcode。简单题。题目:Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The原创 2013-11-26 23:36:43 · 1018 阅读 · 0 评论 -
数组跳跃游戏 Jump Game
题目源自于leetcode。题目:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that positio原创 2013-11-29 19:22:26 · 2676 阅读 · 0 评论 -
寻找最少次跳数 - 贪心VS动态规划 Jump Game 2
非常好的问题:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your原创 2014-03-14 12:35:44 · 4383 阅读 · 0 评论 -
股票最大收益 Best Time to Buy and Sell Stock I
题目源自于leetcode。题目:Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one sha原创 2013-11-20 16:34:28 · 1738 阅读 · 0 评论 -
数组中出现次数超过一半的数字 MoreThanHalfNum
9oj编号1370. 剑指29. 编程之美的发帖水王。普通方法:排序,然后找位于中间位置的数。进阶方法:Hash映射次数。高级方法:擂台PK法。排序法(堆排序)的代码:#include //向堆中加入新元素并调整void MinHeapFixup(int a[], int i){ int j, temp; if(i==0) return;原创 2013-09-28 17:51:11 · 1290 阅读 · 0 评论