2014年4月8日

求连续子数组的最大和

摘要: 题目来源:《剑指offer》面试题31,《编程珠玑》第八章,LeetCode OJ的Maximum Subarray。题目大意:输入一个整型数组,数组里有正数也有负数。数组中一个或连续的多个整数组成一个子数组。求所有子数组的最大和。例如,数组为{1,-2,3,10,-4,7,2,-5},和最大的子数组为{3,10,-4,7,2},因此返回的结果为子数组的和18.来自Google,微软等很多知名公司的面试题,2005年浙江大学计算机系的考研题的最后一道程序设计题,貌似是风靡一时的一道题。参考链接:程序员编程艺术:第七章、求连续子数组的最大和、程序员面试题精选100题(03)-子数组的最大和解题思 阅读全文

posted @ 2014-04-08 23:19 Allen Blue 阅读(211) 评论(0) 推荐(0) 编辑

【LeetCode练习题】Gas Station

摘要: Gas StationThere areNgas stations along a circular route, where the amount of gas at stationiisgas[i].You have a car with an unlimited gas tank and it costscost[i]of gas to travel from stationito its next station (i+1). You begin the journey with an empty tank at one of the gas stations.Return the s 阅读全文

posted @ 2014-04-08 18:16 Allen Blue 阅读(136) 评论(0) 推荐(0) 编辑

再来看看快速排序

摘要: #include #include #include using namespace std;void quickSort(vector &vec,int l,int u){ int i , m; if( l>=u) return ; m = l; for(i = l+1; i vec){ vector::iterator it = vec.begin(); for(; it != vec.end(); it++) cout vec; for(int i = 1; i = u) return; 4 m = l; 5 f... 阅读全文

posted @ 2014-04-08 13:52 Allen Blue 阅读(265) 评论(0) 推荐(0) 编辑

【LeetCode练习题】First Missing Positive

摘要: First Missing PositiveGiven an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space.题目意思:返回第一个缺失的正数。时间复杂度应该在O(n)且只能使用常数的额外空间。解题思路:遍历两遍数组,第一遍的目的是使数组下标为 i 的数值设置为 i+1,如没有 i+1 就是 阅读全文

posted @ 2014-04-08 00:07 Allen Blue 阅读(136) 评论(0) 推荐(0) 编辑

导航