题目:给出一个单链表,返回倒数第K个结点,最后一个结点为倒数第1个。
《剑指offer》上面给的解法是设置两个指针,这里记为p1、p2,先让p2走(k-1)步,然后p1、p2同时走,当p2走到最后一个结点时,p1所指向的结点就是倒数第k个结点。
我觉得按照这样的逻辑写代码反而更容易出错,因为我们需要把我两件重要的问题:(1).p2先走(k-1)步;(2)循环结束的条件是p2到达最后一个结点,即p2->next==NULL。显然这样不太容易控制,我的想法是:先让p2先走k步,然后p...
分类:
其他好文 时间:
2014-08-04 14:22:17
阅读次数:
232
今天看到剑指offer上的第12题,题目如下:
输入数字n,按顺序打印出从1到最大的n位十位数。比如输入3,则打印出1,2,3,...,999。
当我看到这个题目的时候,第一感觉就是用递归,为什么呢?首先得从我们的一个实际数字出发,比如123,我们对数字加1,实际上分为如下两个步骤:
步骤1:最低位加1;
步骤2:若发生进位,则向更高位传播该进位的影响(这也是递归的所在)。
在完成这个算法题之前,我想插一句“细节决定成败!”,我们写一个程...
分类:
其他好文 时间:
2014-08-04 11:07:57
阅读次数:
256
http://acm.hdu.edu.cn/showproblem.php?pid=1203
I NEED A OFFER!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15759 Accepted Submissio...
分类:
其他好文 时间:
2014-08-03 23:28:46
阅读次数:
268
约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。
方法1:使用stl::list模拟环形链表,参考剑指offer
代码:
#include
#include
using namespace std;
i...
分类:
其他好文 时间:
2014-08-03 20:40:55
阅读次数:
218
A Simple Problem with IntegersTime Limit:5000MS Memory Limit:131072KCase Time Limit:2000MSDescriptionYou haveNintegers,A1,A2, ... ,AN. You need to d.....
分类:
其他好文 时间:
2014-08-03 17:59:25
阅读次数:
213
Description
You have N integers, A1, A2, ... ,
AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other ...
分类:
其他好文 时间:
2014-08-03 12:53:25
阅读次数:
311
Dungeon MasterTime Limit:1000MSMemory Limit:65536KTotal Submissions:16468Accepted:6395DescriptionYou are trapped in a 3D dungeon and need to find the ...
分类:
其他好文 时间:
2014-08-02 12:12:43
阅读次数:
220
与折半查找是同一个模式,不同的是,在这里不在查找某个确定的值,而是查找确定值所在的上下边界。def getBounder(data, k, start, end, low_bound = False): if end > 1 if data[ mid ] > k: end = mid - 1 ...
分类:
其他好文 时间:
2014-08-02 10:01:53
阅读次数:
208
In this challenge you need to print the data that accompanies each integer in a list. In addition, if two strings have the same integers, you need to ...
分类:
其他好文 时间:
2014-08-01 22:40:42
阅读次数:
323
Below tables contains contant you need to consider while planning for a CDB.ActionConsiderations for a CDBAdditional InformationPlan the tables and in...
分类:
数据库 时间:
2014-07-31 20:06:07
阅读次数:
401