摘要: 求输入数组的全排列输出。我是用经典的递归思路求解的:1. 当输入数组num只有一个元素,将num压入ret, 返回;(递归终止条件)2. 假设num中的元素个数为N,取出num的最后一个元素,记为b求出num[0..N-2]的全排列,将b插入所有可能的位置,得到完整的全排列结果这道题还有其它思路,已经有人做了讨论,戳这里Given a collection of numbers, return all possible permutations.For example,[1,2,3]have the following permutations:[1,2,3],[1,3,2],[2,1,3], 阅读全文
posted @ 2013-10-25 13:59 Apprentice.Z 阅读(316) 评论(0) 推荐(0)
摘要: 链表题again. 实现链表节点的循环移位. 注意k的边界范围。思路:1. 得到链表长度N,指针end指向尾节点2. 找到新链表头节点在老链表中的前驱节点,记为p. p = N-k%N-13. end指向原链表head节点,此时链表为环状。更新链表头节点位置,head指向p->next。将p->next置为NULL,成为尾节点。Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLan 阅读全文
posted @ 2013-10-25 13:54 Apprentice.Z 阅读(143) 评论(0) 推荐(0)