随笔分类 -  ACM

一些面试及ACM算法题目
摘要:题目描述Given a stringS, find the longest palindromic substring inS. You may assume that the maximum length ofSis 1000, and there exists one unique longes... 阅读全文
posted @ 2015-06-11 11:34 xlturing 阅读(405) 评论(0) 推荐(0)
摘要:题目Given an integern, return the number of trailing zeroes inn!.Note:Your solution should be in logarithmic time complexity.分析Note中提示让用对数的时间复杂度求解,那么如果粗... 阅读全文
posted @ 2015-05-11 11:19 xlturing 阅读(679) 评论(0) 推荐(0)
摘要:Given a list of non negative integers, arrange them such that they form the largest number.For example, given[3, 30, 34, 5, 9], the largest formed num... 阅读全文
posted @ 2015-05-03 12:48 xlturing 阅读(2117) 评论(0) 推荐(0)
摘要:在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目Given a range [m, n] where 0 0 || n > 0) { 8 if ((m & 1) == (n & 1)) { 9... 阅读全文
posted @ 2015-04-29 16:41 xlturing 阅读(540) 评论(0) 推荐(0)
摘要:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two nu... 阅读全文
posted @ 2015-04-29 09:30 xlturing 阅读(533) 评论(0) 推荐(0)
摘要:Description:Count the number of prime numbers less than a non-negative number,nclick to show more hints.Credits:Special thanks to@mithmattfor adding t... 阅读全文
posted @ 2015-04-28 21:21 xlturing 阅读(664) 评论(0) 推荐(0)
摘要:快速排序,只要学习过编程的人肯定都听说过这个名词,但是有时候写的时候还真蒙住了,网上搜罗了下以及查阅了"introduction to algorithm",暂时找到两种实现快排的方式,记录如下:1.通过挖坑,分治的方式,需要左右交替遍历思想如下:代码实现: 1 public static ... 阅读全文
posted @ 2015-04-28 10:56 xlturing 阅读(1719) 评论(0) 推荐(0)
摘要:题目如下:题目给出的例子不太好,容易让人误解成不断顺着右节点访问就好了,但是题目意思并不是这样。换成通俗的意思:按层遍历二叉树,输出每层的最右端结点。这就明白时一道二叉树层序遍历的问题,用一个队列来处理,但是问题是怎么来辨别每层的最右端结点,我思考了半天,最后想出的办法是利用一个标记位,例如上面的例... 阅读全文
posted @ 2015-04-26 23:24 xlturing 阅读(1089) 评论(0) 推荐(0)
摘要:题目:House RobberYou are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constra... 阅读全文
posted @ 2015-04-22 16:45 xlturing 阅读(461) 评论(0) 推荐(0)
摘要:Given a 2d grid map of'1's (land) and'0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent ... 阅读全文
posted @ 2015-04-09 23:08 xlturing 阅读(513) 评论(0) 推荐(0)
摘要:按位操作符只能用于整数基本数据类型中的单个bit中,操作符对应表格:OperatorDescription&按位与(12345&1=1,可用于判断整数的奇偶性)|按位或^异或(同假异真)~非(一元操作符)&=,|=,^=合并运算和赋值>N右移N位,(正数:高位补0,负数高位补1)>>>N无符号右移(... 阅读全文
posted @ 2015-03-31 20:02 xlturing 阅读(922) 评论(0) 推荐(0)
摘要:比较简单的题目,题目大意就是将n个数字围成一个圈,找到一个划分,是的划分左边的数字之和等于右边的数字之和:e.g 10 1 2 2 5,那么可以找到一个划分10 | 1 2 2 5使得两边数字之和都等于10见ac代码#include int main(){ int g,N[31]; while(scanf("%d",&g)!=EOF&&g) { int i,j; for(i=1;i<=g;i++) scanf("%d",&N[i]); int sam=0,ella=0; for(i=1,j=g;i<... 阅读全文
posted @ 2013-10-17 10:42 xlturing 阅读(346) 评论(0) 推荐(0)
摘要:这道题目还是简单的,但是自己WA了好几次,总结下:1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结题目要求输入的格式:STARTX Y ZEND这算做一个data set,这样反复,直到遇到ENDINPUT。我们可以先吸纳一个字符串判断其是否为ENDINPUT,若不是进入,获得XYZ后,吸纳END,再进行输出结果2.注意题目是一个圆周,所以始终用锐角进行计算,即z=360-z;3.知识点的误区:浮点数截断double data;printf("%d",int(data)); //强制类 阅读全文
posted @ 2013-10-15 09:40 xlturing 阅读(291) 评论(0) 推荐(0)
摘要:水题,输出的时候注意下#include #include int main(){ int d; scanf("%d",&d); while(d--) { int binary[30]; int n,i=0; scanf("%d",&n); while(n!=0) { binary[i++]=n%2; n/=2; } int flag=1; for(int j=0;j<i;j++) { ... 阅读全文
posted @ 2013-10-13 19:06 xlturing 阅读(301) 评论(0) 推荐(0)
摘要:题目不难,主要说下这道题目在输入终止上的问题:题目要求当输入为0时一次case结束,当输入为#时整个输入全部结束,可以用如下格式解决while(scanf("%s",str)!=EOF&&strcmp(str,"#")){ if(!strcmp(str,"0")) { //做出相应处理 continue; }}见ac代码#include #include #include #include using namespace std;int main(){ int mile; char type[10... 阅读全文
posted @ 2013-10-10 18:38 xlturing 阅读(385) 评论(0) 推荐(0)
摘要:这是一道很简单的题目,题目大概意思说下:就是有n个监狱(编号从1到n),第一次全部打开,第二次打开编号为2的倍数的,第三次打开编号为3的倍数的,以此类推。。。最后问你有几个监狱是打开的题目中我使用了memset函数来为数组初始化,发现了自己一个以前疏忽的地方1.void * memset(void*,int,size_t)这里的size需要注意是以字节为单位,所以当你为个数为n的整型数组赋初值时要这样写memset(name,1,n*sizeof(int));2.memset在头文件string.h中3.memset只能赋初值0或者1(这个好理解,因为计算机中bit只有这两种状态)代码:#in 阅读全文
posted @ 2013-10-08 16:33 xlturing 阅读(307) 评论(0) 推荐(0)
摘要:这道题目我一开始一头雾水,怎么都数不对,参考了下网上的博文,才弄懂。题意是这样的,如果是上升序列,上升序列的长度不是所有上升数字的,是这么规定的,如果它与前一个数字构成上升,那么这个数字算上长度。所以说,比如1 2 3 4 5 ,这个长度为4。(这里感觉蛮奇怪的,理解了这里就基本ok了),举个例子:1 2 3 2 3 4 5,上升序列有1 2 3 , 2 3 4 5 ,长度为2 + 3 = 5,个数为2,所以输出5.0/2.0 = 2.5如果有重复数字,比如4 4 4 3 3 3 3 ,那么这整个是下降序列;再如4 4 4 3 3 3 4 ,则前6个是下降,后俩是上升。见ac代码#includ 阅读全文
posted @ 2013-10-06 16:43 xlturing 阅读(384) 评论(0) 推荐(0)
摘要:这道题目的关键在于怎么求两个整数的最大公约数,这里正好复习一下以前的知识,如下:1.设整数a和b2.如果a和b都为0,则二者的最大公约数不存在3.如果a或b等于0,则二者的最大公约数为非0的一个4.如果b不为0,则使得a=a,b=a%b,转到2重复执行实现的递归代码如下:int gcb(int a,int b){ if(b==0) return a; else return gcb(b,a%b); }注:这个算法的证明这里简单说明下:1.设g为a和b的公约数2.则存在m和k使得 a=g*m b=g*k3.同时利用b可表示a a=b*l+r (其中r为... 阅读全文
posted @ 2013-10-05 23:43 xlturing 阅读(481) 评论(0) 推荐(0)
摘要:这道题目不难,是一道简单的进制转换问题,但是发现了自己两个遗漏的知识点:1.关于scanf(1)scanf函数在输入时是以回车或者空格作为一次输入的结束(2)scanf函数在输入字符串的过程中是不吸纳空格的,而且如果输入是整数或者浮点数,前面的空格对输入没有影响,如下: char s1[100],s2[100]; int a,b; scanf("%s%s%d%d",s1,s2,a,b); printf("%s%s%d%d",s1,s2);如上面这段代码,如果给定输入为:" ACM ACM 1 2 "则输出为:"ACMACM1 阅读全文
posted @ 2013-10-04 20:52 xlturing 阅读(485) 评论(0) 推荐(0)
摘要:zju对时间要求比较高,这就要求我们不能简单地暴力求解(三个循环搞定),就要换个思路:因为在循环时,已知a,确定b,c,d,在外重两层循环中已经给定了b和c,我们就不用遍历d,我们可以利用d^3=a^3-b^3-c^3来判断这个d。看代码:#include #include int main(){ for(int n=2;nresult) break; double t=result-ai-bj; double ck=pow(t,1.0/3.0); ... 阅读全文
posted @ 2013-09-28 22:07 xlturing 阅读(467) 评论(0) 推荐(0)