摘要: 题目描述 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 解题思 阅读全文
posted @ 2017-05-11 13:15 qqky 阅读(211) 评论(0) 推荐(0) 编辑
摘要: 题目描述 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。 解题思路:首先要注意特殊情况的处理 base为0情况 exponent为0 为1情况 exponent为负数情况 1 #include <iostream> 2 using na 阅读全文
posted @ 2017-05-11 10:18 qqky 阅读(160) 评论(0) 推荐(0) 编辑
摘要: 题目描述 输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。 解题思路: 方法一:n&flag 其中flag为1 然后每次与之后flag=flag<<1;这种方式是为了避免负数导致出现的死循环 方法二:n&(n-1) 此时是将n的右面最后一位1变为0,也就是去掉了一个1,重复操作直至n 阅读全文
posted @ 2017-05-10 14:44 qqky 阅读(212) 评论(0) 推荐(0) 编辑
摘要: 题目描述 Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given1->2->3->4->5->NULLand k =2, return4->5->1->2- 阅读全文
posted @ 2017-05-10 10:53 qqky 阅读(326) 评论(0) 推荐(0) 编辑
摘要: 题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 解题思路:得到f(n)=f(n-1)+f(n-2) 1 #include <iostream> 2 using namespace std; 3 class S 阅读全文
posted @ 2017-05-10 09:36 qqky 阅读(207) 评论(0) 推荐(0) 编辑
摘要: 题目描述 Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up: Can you solve it without using extra sp 阅读全文
posted @ 2017-05-09 15:06 qqky 阅读(205) 评论(0) 推荐(0) 编辑
摘要: 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。 解题思路:可知本题f(n)=f(1)+f(2)+...+f(n-1)+1;通过数学归纳法得到f(n)=2^(n-1) 1 #include <iostream> 2 using 阅读全文
posted @ 2017-05-09 10:07 qqky 阅读(156) 评论(0) 推荐(0) 编辑
摘要: 题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。 解题思路:f(n)=f(n-1)+f(n-2);且0 1 2需要单独处理 1 #include <iostream> 2 using namespace std; 3 class Solution 阅读全文
posted @ 2017-05-09 09:57 qqky 阅读(173) 评论(0) 推荐(0) 编辑
摘要: 题目描述 Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 解题思路:使用快慢指针,若在遍历过程中slow==fast则有环,否则 阅读全文
posted @ 2017-05-08 14:59 qqky 阅读(132) 评论(0) 推荐(0) 编辑
摘要: 题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。 n<=39 解题思路:首先如果直接return Fibonacci(n-1)+Fibonacci(n-2)的话会有很多值算了很多遍;因此思考如何遍历一遍得到结果。首先0和1需要单独计算,然后从2开始f2 = f 阅读全文
posted @ 2017-05-08 10:13 qqky 阅读(148) 评论(0) 推荐(0) 编辑