![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
模板
peace in mind
这个作者很懒,什么都没留下…
展开
-
Sicily 1119. Factstone Benchmark
用log函数用把乘法转换为加法。 n! log(1) + log(2) + … + log(n) #include#includeusing namespace std;int main() { int year; while (cin >> year && year) { double bits = pow(2.0, 2 + (year - 1960) / 10原创 2014-12-05 19:23:57 · 467 阅读 · 0 评论 -
[Leetcode] Reverse Linked List
题目链接在此Reverse a singly linked list.反转一个单链表。别小看这道题啊,我TX面试上来就问这道题。当时我觉得用两个指针就够了——显然天真了,得用三个指针。这是一个循环的算法:struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL)原创 2015-08-05 17:19:21 · 436 阅读 · 0 评论 -
[leetcode] Summary Ranges
题目链接在此Given a sorted integer array without duplicates, return the summary of its ranges.For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].题目意思就是把连续的递增1的子序列义字符串的形式表示出来。并不是很难,原创 2015-08-05 15:49:47 · 293 阅读 · 0 评论 -
Sicily 1151. 魔板
题目链接在此经典的状态转换搜索题。一.原题中文大意(1)描述魔板由8个大小相同方块组成,分别用涂上不同颜色,用1到8的数字表示。其初始状态是:1 2 3 48 7 6 5对魔板可进行三种基本操作:A操作(上下行互换):8 7 6 51 2 3 4B操作(每次以行循环右移一个):4 1 2 35 8 7 6C操作(中间四小块顺时针转原创 2015-10-07 19:24:11 · 678 阅读 · 0 评论 -
Sicily. 迷宫最短路径
Time Limit: 1sec Memory Limit:256MBDescription有一个矩形迷宫,入口和出口都确定为唯一的,且分布在矩形的不同边上。现在让你算出最短需要走多少步,才可以从入口走到出口。Input共N+1行,第一行为N(N=0表示输入结束),以下N行N列0-1矩阵,1表示不能通过,0表示可以通过(左上角和右下角为0,即入口和出口),原创 2015-10-07 21:55:40 · 1643 阅读 · 0 评论 -
Sicily 1029. Rabbit【高精度加法】
题目链接在此这道题从题目的描述中可看出端倪。当m=2的时候,每个月兔子的对数构成的序列活脱脱就是斐波那契数列(初始值为1,即a[0]=1;第1个月为2,即a[1]=2;第2个月为3,即a[2]=3;第3个月为5,即a[3]=5……)。仔细找一找规律可以发现,有如下的规律:rabbit[i] =rabbit[i-1] +rabbit[i-m] 而当i<m的时候,rabbi...原创 2014-11-07 19:05:44 · 786 阅读 · 0 评论 -
[Leetcode] Combinations
题目链接在此Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [原创 2015-10-28 11:48:23 · 313 阅读 · 0 评论 -
[Leetcode] Subsets
题目链接在此Given a set of distinct integers, nums, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subs原创 2015-10-23 16:05:24 · 378 阅读 · 0 评论 -
[LeetCode] Permutations I, II
转自这位大神。啥也不说了,看了大神的代码,感觉就像柯南破案的时候,脑袋中有一道闪电划过一样。Permutations I题目链接在此Given a collection of numbers, return all possible permutations.For example,[1,2,3] ha转载 2015-10-30 22:16:14 · 454 阅读 · 0 评论 -
Sicily. 八皇后问题
Description给定一个8*8棋盘,要求在棋盘上放8个皇后,并且各个皇后之间不能相互攻击。 当两个皇后位于同一行或同一列或同一对角线(有两条)就会相互攻击。 现在要求大家编程输出8皇后问题的所有解。 注意:皇后是逐行放的,也就要求输出每一行放皇后的列号的序列,行号和列号都是从1开始的Input无Output每一行输出一个解决方案原创 2015-11-02 17:02:56 · 944 阅读 · 0 评论 -
[Leetcode]Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20原创 2015-03-30 22:28:41 · 419 阅读 · 0 评论 -
Sicily 1171. The Game of Efil 【利用完全二叉树遍历构造序列】
题目链接在此源代码:#include#includeint total;int finalGrid[20][20];const int neiRow[] = { -1, 0, 1, -1, 1, -1, 0, 1 };const int neiCol[] = { -1, -1, -1, 0, 0, 1, 1, 1 };void expand(int grid[][原创 2014-12-24 14:54:54 · 1366 阅读 · 0 评论 -
[leetcode] Implement strStr()
题目链接在此Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.模式匹配。KMP咯。说起KMP的话,自从大一上学C语言开始就有所了解,但是只知道它的用途和大概原理,从没下过功夫落实转载 2015-08-13 11:22:20 · 411 阅读 · 0 评论 -
[leetcode] Implement Queue using Stacks
题目链接在此。Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the fro原创 2015-07-31 15:10:14 · 383 阅读 · 0 评论 -
sicily 1203. The Cubic End
题目链接在此转自这位大神1.乘法求模:(1)模数不大的时候可利用性质(a*b)%c = (a%c)*(b%c)%c(2)模数和两乘数都很大的时候,可用以下算法(暂时还没弄清原理和理论来源,就当个模板吧):long long mulAndMod(long long a, long long b, long long mod) { // a * b % mod转载 2014-12-03 13:06:36 · 939 阅读 · 0 评论 -
[Leetcode]Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe原创 2015-03-30 22:20:30 · 351 阅读 · 0 评论 -
Sicily 1018. A Card Trick【排列组合】
题目链接在此这道题提议有些难理解原创 2014-11-05 00:43:55 · 851 阅读 · 0 评论 -
Sicily 1004. I Conduit!
题目链接在此参考的是这位大神的解题方法源代码和注释:转载 2014-11-06 11:59:26 · 643 阅读 · 0 评论 -
Sicily 1381. a*b【高精度乘法】
题目链接在此原创 2014-11-07 21:02:19 · 1487 阅读 · 0 评论 -
Sicily 1231. The Embarrassed Cryptography【高精度取余】
题目链接在此启示 1用筛选法构造质数表;启示 2memset函数是以字节为单位操作数组的,对于char型数组,第二个参数怎样设置都行,因为每个char刚好就1个字节。但对于int型数组,就不能随便设第二个参数,因为每个int有4个字节。比如想把int a[20]数组的所有值都初始化为1,就不能写memset(a, 1, 20),这样一来每个数组的前5(20/4=5)个元素的原创 2014-12-04 19:39:16 · 963 阅读 · 0 评论 -
Sicily 1099. Packing Passengers【最大公约数】
题目链接在此遍历肯定超时!!!转自这位大神1.设(1)pA:每架A飞机能载的人数,cA:每架A飞机的开销,nA:A飞机的数量(2)pB:每架B飞机能载的人数,cB:每架B飞机的开销,nB:B飞机的数量(3)S:总人数,T:总开销则有:【1】pA * nA + pB * nB = S【2】T = cA* nA + cB * nB转载 2014-12-08 11:57:15 · 634 阅读 · 0 评论 -
Sicily 1028. Hanoi Tower Sequence【高精度取余和除法】
题目链接在此这道题目有点意思。渣渣我从这位大神这里得到了解题奥妙。找某个数的二进制从右边数第一个“1”是第几位即可。由于这题的数可能很大,那么就要使用高精度除法和取余了=。=思路就是模仿手算除法。1020就是道高精度取余的题,与这道题可以互相借鉴。源代码如下:#include#includeusing namespace std;int cha原创 2014-11-06 23:52:54 · 935 阅读 · 0 评论 -
[LeetCode]Reverse Integer
LeetCode的题目一般都很简短,经常要自己想各种可能的测试样例。题目:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321这题坑爹的地方在于1000000003和-2^31这样的数翻转之后会溢出(返回0)。我用的是比较原创 2015-03-30 21:57:34 · 340 阅读 · 0 评论 -
[LeetCode]Majority Element
数组求过半数的元素。做到这道题的时候,心里也是一阵痛,因为上学期算法期末考刚好有这样一道题,而且有空间的限制。当时想着用map来实现,时间上是快啊。但是不用想,那么大的数据,空间肯定会爆。LeetCode上的这个解法只用维护两个变量,而且时间也是O(n):源代码:int majorityElement(vector &num) { int count原创 2015-03-29 21:26:01 · 391 阅读 · 0 评论 -
[leetcode] Implement Stack using Queues
题目链接在此。Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty原创 2015-07-31 16:12:08 · 351 阅读 · 0 评论 -
Sicily. 完整计算器(浮点数;四则运算与括号)
Time Limit: 1sec Memory Limit:256MBDescription 请实现一个只包含加减乘除以及括号运算的简单计算器,忽略式子中的所有空格,输入格式是正确的,不需要判断。Input 第一行只有一个整数m,代表有m个测试用例接下来有m行,每一行是一个测试用例,代表要求计算的式子,请注意输入的数字可能有小数。原创 2015-10-09 17:24:11 · 2951 阅读 · 0 评论