经历了三道树的题后,完全崩溃中,急需一道非树图的题来挽救信心。题目:反转数字。input : 123
, output : 321.思路:直接,没什么好说的。自己代码:很龊,有大量的冗余信息,还申请了一个List,虽然AC了,但有很大改进空间。public
int reverse(int x) { ...
分类:
其他好文 时间:
2014-05-15 14:22:31
阅读次数:
212
题意:给定数字n,生成所有可能的n对括号的组合
思路:dfs暴力枚举
当左括号出现的次数 < n 的时候,可以选择放置新的左括号
当右括号出现的次数 < 左括号的次数的时候,可以选择放置新的右括号
递归函数:
void generateparenthesis(int n, int left, int right, vector& result)
表示将生成的所有可能的n对括号的组合放到result中,
其中,
s表示已生成的部分括号,
left表示当前的左括号数,
right表示当前的右括号...
分类:
其他好文 时间:
2014-05-15 13:21:12
阅读次数:
311
这道题是我最初刷的那20多道之一,但一直没有过,被各种各样的情况折磨死了,直到把所有其他的题都写完,回来看大神对这道题是怎么处理的时候,才惊叹算法的奇妙。再次验证了我的想法,如果要处理各种各样的特殊情况,一定是算法本身有问题。
之前看过很多有关在两个排序数组中找中位数的解法,大多根据两个数组长度不同分了很多种情况,各种讨论。下面要介绍的方法并没有直接求中位数,而是把求中位数转换成了求两个数组合并...
分类:
其他好文 时间:
2014-05-15 12:21:05
阅读次数:
273
Two SumGiven 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...
分类:
其他好文 时间:
2014-05-15 10:48:59
阅读次数:
322
class Solution {public: bool search(int A[],
int n, int target) { if (n A[mid]) { right = mid; sep = A[mid]; ...
分类:
其他好文 时间:
2014-05-15 09:29:52
阅读次数:
177
题意:交换给定链表中的相邻节点,但不可以改变链表里的值
如1->2->3->4交换后为2->1->4->3
思路:
按题意中的扫描去改变每两个相邻节点的next指针的指向即可。
小技巧:
因为处理每两个相邻节点的时候,需要一个指针记录它们前一个节点,而头节点前面没有节点,
所以可设置一个dummy节点指向头指针,这样开头的两个节点的处理方式跟其它的相邻节点的处理方式就一样了
复杂度:时间O(n),空间O(1)...
分类:
其他好文 时间:
2014-05-15 00:16:00
阅读次数:
359
Linked List Cycle
Total Accepted: 17148 Total
Submissions: 49300My Submissions
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra sp...
分类:
其他好文 时间:
2014-05-15 00:13:50
阅读次数:
279
[ 问题: ]
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 numbers such that they add up to the ta...
分类:
其他好文 时间:
2014-05-15 00:04:39
阅读次数:
377
1.注意规则
2.注意最小的负数的绝对值比最大的正数的绝对值大1,所以 line 13: (str[i] - '0') > INT_MAX%10不能取等号...
分类:
其他好文 时间:
2014-05-14 23:52:26
阅读次数:
478
Roman to Integer 罗马数字转化为整数...
分类:
其他好文 时间:
2014-05-14 23:49:52
阅读次数:
515