
algorithm
文章平均质量分 62
代码与单车
这个作者很懒,什么都没留下…
展开
-
【算法】从1到n的正数中0的出现次数
准备找工作了,上次搞这些算法数据结构还是研究生复试上机考试。现在必须得重操旧业了。加油!写的很简单,能实现功能,但是需要改进,接下来再研究。int countZero(int data){ int count = 0; for(int i=1; i<=data; i++){ int d = i; while(d > 0){原创 2013-04-02 22:17:18 · 1951 阅读 · 0 评论 -
Code Jam 2010 Round 1A Problem C
Problem C. Number GameProblemArya and Bran are playing a game. Initially, two positive integers A and B are written on a blackboard. The players take turns, starting with Arya. On his or her t原创 2013-09-20 12:39:24 · 665 阅读 · 0 评论 -
Code Jam 2010 Round 1B Problem A
Problem B. Picking Up ChicksProblemA flock of chickens are running east along a straight, narrow road. Each one is running with its own constant speed. Whenever a chick catches up to the one i原创 2013-09-20 14:54:17 · 654 阅读 · 0 评论 -
Code Jam 2010 Round 1B Problem B
Problem B. Picking Up ChicksProblemA flock of chickens are running east along a straight, narrow road. Each one is running with its own constant speed. Whenever a chick catches up to the one i原创 2013-09-20 18:28:27 · 637 阅读 · 0 评论 -
Code Jam 2010 Round 1B Problem C
Problem C. Your Rank is PureProblemPontius: You know, I like this number 127, I don't know why.Woland: Well, that is an object so pure. You know the prime numbers.Pontius: Surely I do. T原创 2013-09-21 11:41:55 · 705 阅读 · 0 评论 -
找两个链表的公共节点
首先考虑两个链表无环的情况。将链表a的尾节点指向头节点从而形成环。用快慢指针遍历链表b,一个一次移动2单位,另一个移动1单位。如果不相遇则不存在公共节点。如果相遇,则让其中一个指针指向b,两个指针以1单位/次的速度移动,直到相遇。相遇时指向的节点就是公共节点的起始。最后记得将a的尾节点恢复。代码如下。其次考虑有环的情况。用快慢指针探测a中是否有环。如果有则,将使p指向a的尾节点,p的nex原创 2013-09-21 18:18:45 · 2328 阅读 · 0 评论 -
卡特兰数相关问题
卡特兰数:规定C0=1,而C1=1,C2=2,C3=5,C4=14,C5=42,C6=132,C7=429,C8=1430,C9=4862,C10=16796,C11=58786,C12=208012,C13=742900,C14=2674440,C15=9694845公式为Cn=C(2n, n)/(n+1)=C(2n, n)-C(2n, n-1)n推倒过程C(原创 2013-09-22 23:23:40 · 914 阅读 · 0 评论 -
Google China New Grad Test 2014 Round A Problem B
Problem B. Rational Number TreeProblemConsider an infinite complete binary tree where the root node is 1/1 and left and right childs of node p/q are p/(p+q) and (p+q)/q, respectively. This tre原创 2013-09-23 16:11:33 · 1059 阅读 · 0 评论 -
Google China New Grad Test 2014 Round A Problem C
Problem C. SortingProblemAlex and Bob are brothers and they both enjoy reading very much. They have widely different tastes on books so they keep their own books separately. However, their fat原创 2013-09-23 16:16:34 · 810 阅读 · 0 评论 -
Google China New Grad Test 2014 Round A Problem A
Problem A. Read Phone NumberProblemDo you know how to read the phone numbers in English? Now let me tell you.For example, In China, the phone numbers are 11 digits, like: 15012233444. Someon原创 2013-09-23 16:08:07 · 830 阅读 · 0 评论 -
Google China New Grad Test 2014 Round A Problem D
Problem D. Cross the mazeProblemEdison, a robot, does not have a right hand or eyes. As a brave robot, he always puts his left hand on the wall no matter he walks or turns around. Because he t原创 2013-09-23 16:24:23 · 1349 阅读 · 2 评论 -
Google China New Grad Test 2014 Round A Problem E
Problem E. Spaceship DefenceProblemThe enemy has invaded your spaceship, and only superior tactics will allow you to defend it! To travel around your spaceship, your soldiers will use two devi原创 2013-09-23 20:53:56 · 1317 阅读 · 0 评论 -
2013亚马逊校招机试题2
http://discuss.leetcode.com/questions/223/jump-game-ii题目参考这里,我觉得是个动态规划问题,写出的解法最终2/10个用例失败。做题太投入,看错了有道的面试时间,痛心疾首啊! static int walk(int[] array) { int[] t = new int[array.length]; Arrays.fill(原创 2013-09-27 15:37:06 · 1113 阅读 · 0 评论 -
2013亚马逊校招机试题1
题目没复制下来,凭印象写的。1. 删除代码中的的注释。注释分两种,//和/**/。字符串中出现的注释符号不能被当作注释。解题思路:以前编译原理课做过类似的程序,分出几个状态,并做状态转移判断即可。StringBuilder中存放返回的字符串。动作a表示添加当前字符到StringBuilder,b表示删除StringBuilder中最后一个字符。-表示其他字符或者无动作或者状态不转变原创 2013-09-27 15:33:51 · 933 阅读 · 0 评论 -
Code Jam 2010 Round 1A Problem B
Problem B. Make it SmoothProblemYou have a one-dimensional array of N pixels. Each pixel has a value, represented by a number between 0 and 255, inclusive. The distance between two pixels is t原创 2013-09-18 14:35:28 · 861 阅读 · 0 评论 -
Code Jam 2010 Round 1A Problem A
https://code.google.com/codejam/contest/544101/dashboard#s=p0Problem A. RotateProblemIn the exciting game of Join-K, red and blue pieces are dropped into an N-by-N table. The table stands原创 2013-09-18 13:33:15 · 722 阅读 · 0 评论 -
【算法】Longest Increasing Subsequence
看微软笔试题遇到的。Longest Increasing Subsequence(LIS) means a sequence containing some elements in another sequence by the same order, and the values of elements keeps increasing.For example, LIS of {2,1,原创 2013-04-05 18:38:46 · 605 阅读 · 0 评论 -
微软2013暑期实习生笔试题错误
原题参见http://www.cnblogs.com/justcxtoworld/archive/2013/04/06/3002719.html可能有错误的答案参见http://bbs.byr.cn/#!article/Job/1372053错题分析:3. 【ABC】少选择了A。看到已排序查找就想到了顺序查找,如果array使用二分查找,那么时间复杂度就不同了。5. 不定。难道是原创 2013-04-07 12:20:50 · 1203 阅读 · 0 评论 -
2013编程之美全国挑战赛-树上的三角形
http://programming2013.cstnet.cn/qualification/problem/3思路很简单,Dijkstra算法求出最短路径,暂没考虑多条路径的情况。然后三角形判断。可是最后Wrong Answer。import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;i原创 2013-04-08 20:39:35 · 904 阅读 · 2 评论 -
2013编程之美全国挑战赛-传话游戏
http://programming2013.cstnet.cn/qualification/problem/1题目很简单,理解上开始没注意到N的作用。import java.util.HashMap;import java.util.Scanner;public class Ex1 { public static void main(String[] args) { S原创 2013-04-08 20:41:29 · 714 阅读 · 0 评论 -
主方法求解递归算法复杂度
假设有递推关系式 1" src="http://upload.wikimedia.org/math/5/b/1/5b1c03811f902a398db6d4de2c55fa55.png" style="border:none; vertical-align:middle">其中,n为问题规模,a为递推的子问题数量,n/b为每个子问题的规模(假设每个子问题的规模基本一样),f(n转载 2013-03-28 15:35:57 · 975 阅读 · 0 评论 -
2013编程之美全国挑战赛初赛第二场-集会
昨天做编程之美的题感觉只有这一道是水题。思路没问题但是写程序写错了一个地方没AC。今天翻出来想了一下终于解决了。解题思路: 要寻找的这个目标点的纵坐标为0,设横坐标为x。以示例数据为例,可以得到目标点到这些点的距离,更直观一点,绘制成图形点击查看。观察可知符合要求的点可能出现的位置是某两个抛物线的交点或者某个抛物线的顶点。求出这些点来比较计算出的距离,取最小的即可。没机会提交的代原创 2013-04-15 12:43:05 · 877 阅读 · 0 评论 -
Maximum Subarray Algorithm
算法导论4.2-5提供了一种线性时间内解决的方法。思路是,最大子串有一个特性,从前向后逐个累加,过程中和始终为正。这是因为如果计算到某个数,和为负,则去掉包括这个数的前面这部分,剩下的子串和会更大,这和之前认为的最大子串是冲突的。因此从前往后逐个累加求和,如果和为负,则从下一个开始重新累加。在这个过程中找出和的最大值即可。package cpt4;import Utils.P;原创 2013-08-28 23:34:58 · 684 阅读 · 0 评论 -
Google 2014校招测试赛 Problem C
Problem C. MoistProblemMoist has a hobby -- collecting figure skating trading cards. His card collection has been growing, and it is now too large to keep in one disorganized pile. Moist needs原创 2013-09-14 23:49:22 · 798 阅读 · 0 评论 -
Google 2014校招测试赛 Problem A
Problem A. Bad HorseProblemAs the leader of the Evil League of Evil, Bad Horse has a lot of problems to deal with. Most recently, there have been far too many arguments and far too much backstabbi原创 2013-09-14 23:22:01 · 860 阅读 · 0 评论 -
Google 2014校招测试赛 Problem B
Problem B. Captain HammerProblemThe Hamjet is a true marvel of aircraft engineering. It is a jet airplane with a single engine so powerful that it burns all of its fuel instantly during takeoff.原创 2013-09-14 23:45:18 · 1635 阅读 · 0 评论 -
东京大学ACM Java模板
做ACM题还是觉得Java更熟悉,code效率更高。找到了东京大学的模板,为了适应Google Code Jam,加入了TO_FILE来控制输出到控制台还是文件。之前做的code jam的一些练习题,想看别人Java的代码还真难,最后找到了一个日本人的代码是Java的。看来Java在日本使用很广啊。import static java.lang.Math.*;import static ja原创 2013-09-14 23:59:04 · 4762 阅读 · 0 评论 -
创新工场的一道笔试题
整个卷子出错的问题是堆排序,堆调整有一个参数写错了。几周不看,就出错了,看来考前一定要温习。不会的题是写手动开平方。 static double sqrt(int a, int k) { double x = 1.0; double d = (k - 1.0) / k; double ox = 0; while (Math.abs(ox - x) > 0.0000001原创 2013-09-16 23:09:20 · 862 阅读 · 0 评论 -
1-0组成的串中,1和0个数相等的最长子串
同学面试遇到的题,我以尽量简单的方式描述,偶子串这个概念是为了表述创造的概念。Problem如果一个串只由1和0组成,并且其中1和0的个数相等,我们称之为偶子串。给出一个只由1和0组成的串,求这个串中的子串中,最长的偶子串。Input第一行是N,测试用例的总数,接下来是N行1和0组成的串OutputN行结果,最长偶子串可能有多SampleInput5原创 2013-09-17 11:12:49 · 1890 阅读 · 0 评论 -
不使用递归求全排列和组合数
同学遇到的面试问题,大意是M台机器放在N个房间,不使用递归求打印所有情况解题思路:情况共计N**M种。开始想将所有情况放入数组,填充好数组再逐个打印。随后发现按照填充的思路,不使用大数组也可以实现。思路是加入M=N=3,则27种情况,记i0...i26。0...M个数,0放入i0[0],i1[1],i2[2],i3[0],i4[1],i5[2]...,1放入i0[,i1,i2,原创 2013-09-27 16:54:01 · 1055 阅读 · 0 评论