算法
文章平均质量分 70
zll00561
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
3.2 CountDiv
Write a function: class Solution { public int solution(int A, int B, int K); } that, given three integers A, B and K, returns the number of integers within the range [A..B] that are divisible by K, i原创 2015-04-10 09:31:43 · 543 阅读 · 0 评论 -
5.2 Nesting
Determine whether given string of parentheses is properly nested. 需要注意空间复杂度为1,不能使用stack。 A string S consisting of N characters is called properly nested if: S is empty; S has the form “(U)” where U原创 2015-04-16 09:11:32 · 990 阅读 · 0 评论 -
MaxCounters
You are given N counters, initially set to 0, and you have two possible operations on them: increase(X) − counter X is increased by 1, max counter − all counters are set to the maximum value of any c原创 2015-04-09 15:38:43 · 576 阅读 · 0 评论 -
1. PermCheck Check whether array A is a permutation.
A non-empty zero-indexed array A consisting of N integers is given. A permutation is a sequence containing each element from 1 to N once, and only once. For example, array A such that: A[0] = 4原创 2015-04-09 10:23:12 · 695 阅读 · 0 评论 -
3.1 PassingCars
A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road. Array A contains only 0s and/or 1s: 0 represents a car tr原创 2015-04-09 21:15:30 · 827 阅读 · 0 评论 -
4.4 StoneWall
求一堵高低不平的墙,最少可以用多少块砖。砖可以是任意高度宽度的正方形。 You are going to build a stone wall. The wall should be straight and N meters long, and its thickness should be constant; however, it should have different heights原创 2015-04-17 21:24:56 · 719 阅读 · 0 评论 -
8.3 Peaks
A non-empty zero-indexed array A consisting of N integers is given.A peak is an array element which is larger than its neighbors. More precisely, it is an index P such that 0 < P < N − 1, A[P − 1] < A原创 2015-04-30 21:00:06 · 549 阅读 · 0 评论 -
9.1 ChocolatesByNumbers
N块巧克力摆成一个环从0到N-1。首先吃No.0块,然后吃掉No.M块,依次吃掉No.2M….直到遇到空的块,求可以吃掉几块。 求最小公倍数的问题。class Solution { public int solution(int N, int M) { // write your code in Java SE 8 // x*M % N = y*M %N原创 2015-05-04 20:54:51 · 1648 阅读 · 0 评论 -
8.4 Flags
// you can also use imports, for example: import java.util.*;// you can use System.out.println for debugging purposes, e.g. // System.out.println(“this is a debug message”);class Solution {原创 2015-04-30 16:59:13 · 550 阅读 · 0 评论 -
cargo center
将某城市划分为m*n个区域,每个区域货物量保存在二维数组。将每个区的货物送到一个cargo center,汽车只能上,下,左,右,四个方向移动。求center使所有货物运到这儿的cost最小。初中物理有道题,给你一块不均匀木板,如何找到它的重心。用一根绳吊起来,画一条垂直线,再换个地方吊起了画出垂直线。两线交叉的地方就是重心。 这道题也是一样,先找出最小的列使所有点到这一列的cost最小。再找到行原创 2015-05-04 10:48:48 · 698 阅读 · 0 评论 -
5.1 Brackets
(),[],{}配对的问题,使用stack的经典案例,不多说了。 强烈吐槽java泛型就是一坨屎。我又想换C#了,单就语言来说C#明显比java友好多了,后发优势吧。前浪都死在沙滩上了。最近的趋势是函数式编程语言。JS,D语言都有浓重的函数式味道class Solution { public int solution(String S) { // write your co原创 2015-04-16 15:05:50 · 410 阅读 · 0 评论 -
5.3 Fish eat fish
N voracious fish are moving along a river. Calculate how many fish are alive. You are given two non-empty zero-indexed arrays A and B consisting of N integers. Arrays A and B represent N voracious fis原创 2015-04-16 14:44:33 · 854 阅读 · 0 评论 -
MissingInteger
Write a function: class Solution { public int solution(int[] A); } that, given a non-empty zero-indexed array A of N integers, returns the minimal positive integer that does not occur in A. For exam原创 2015-04-09 16:43:43 · 727 阅读 · 0 评论 -
7.1 MaxProfit
求股票最大收益。算法导论上的问题 A zero-indexed array A consisting of N integers is given. It contains daily prices of a stock share for a period of N consecutive days. If a single share was bought on day P and sold原创 2015-04-21 16:28:23 · 490 阅读 · 0 评论 -
6.2 Dominator
如果存在leader,返回其任意index,否则返回-1. A zero-indexed array A consisting of N integers is given. The dominator of array A is the value that occurs in more than half of the elements of A. For example, consider原创 2015-04-18 22:13:28 · 478 阅读 · 0 评论 -
7.2 MaxSliceSum
求连续最大和子数组。与例子中不同的是,如果和为负返回最大的数而不是0.Solutution当maxCur+A[i]<0时,maxCur不能设置为0,因为如果所有值都小于0,得到错误结果。在之前程序的基础上,增加分段最大值为负的判断。class Solution { public int solution(int[] A) { int maxSum = Integer.MIN原创 2015-04-22 15:49:40 · 575 阅读 · 0 评论 -
3.3 MinAvgTwoSlice
求数组最小平均数子序列起始位置 A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P < Q < N, is called a slice of array A (notice that the slice contains at原创 2015-04-13 22:06:30 · 671 阅读 · 0 评论 -
7.3 MaxDoubleSliceSum
求两相邻子数组和最大,两端是开区间。 这道思索了很久,觉得解法没问题但是性能测试只有两个通过。 有两个因素影响最大值,两端的边界及切分的位置。边界扫描的同时在段内调整slice位置保证maxL+maxR的和最大。策略是以右边界扫描为主,maxR+A[i-1]<0及A[slice]>A[i-1]时调整slice。 但是为什么不对呢??class Solution { public int原创 2015-04-23 21:21:25 · 546 阅读 · 0 评论 -
4.2 Distinct
找出数组中出现不同元素的个数。import java.util.*;class Solution { public int solution(int[] A) { if(A.length==0) return 0; Arrays.sort(A); int size = 1; int former = 0; fo原创 2015-04-14 16:41:48 · 447 阅读 · 0 评论 -
4.1 MaxProductOfThree
Maximize A[P] * A[Q] * A[R] for any triplet (P, Q, R). 寻找数组A,三个数乘积的最大值。A non-empty zero-indexed array A consisting of N integers is given. The product of triplet (P, Q, R) equates to A[P] * A[Q] * A[R原创 2015-04-14 16:31:37 · 815 阅读 · 0 评论 -
4.4 NumberOfDiscIntersections
Compute intersections between sequence of discs. 数组A[i]保存以i为圆点,A[i]为半径的圆。求多少对圆至少有一个共同点。 Given an array A of N integers, we draw N discs in a 2D plane such that the I-th disc is centered on (0,I) and原创 2015-04-15 16:42:11 · 749 阅读 · 0 评论 -
最大公约数(Gcd)两种算法(Euclid && Stein)转载
写的不错,代码看起来很舒服。欧几里德算法和扩展欧几里德算法 欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数。其计算原理依赖于下面的定理:定理:gcd(a,b) = gcd(b,a mod b)证明:a可以表示成a = kb + r,则r = a mod b 假设d是a,b的一个公约数,则有 d|a, d|b,而r = a - kb,因此d|r 因此转载 2015-05-05 16:40:53 · 540 阅读 · 0 评论
分享