2013年11月3日

N-Queens

摘要: 代码: 1 vector > result; 2 void search(int line, int total, vector &v, vector &col, vector &left, vector &right){ 3 if(line == total){ 4 result.push_back(v); 5 return; 6 } 7 int i; 8 for(i = 0; i > solveNQueens(int n) {23 // IMPO... 阅读全文

posted @ 2013-11-03 22:49 waruzhi 阅读(191) 评论(0) 推荐(0) 编辑

N-Queens II

摘要: 代码: 1 int result; 2 void search(int line, int total, vector &col, vector &left, vector &right){ 3 if(line == total){ 4 result++; 5 return; 6 } 7 int i; 8 for(i = 0; i col(n, true), left(2*n-1, true), right(2*n-1, true);25 searc... 阅读全文

posted @ 2013-11-03 22:46 waruzhi 阅读(199) 评论(0) 推荐(0) 编辑

Reorder List

摘要: Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given{1,2,3,4}, reorder it to{1,4,2,3}.思路:代码: 1 void reorderList(ListNode *head) { 2 // IMPORTANT: Please reset any member data you de... 阅读全文

posted @ 2013-11-03 21:34 waruzhi 阅读(196) 评论(0) 推荐(0) 编辑

Jump Game

摘要: Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index.For example:A =[2,3,1,1,4], returntrue.A =[3,2,1,0,4], returnfalse.思路 阅读全文

posted @ 2013-11-03 21:15 waruzhi 阅读(187) 评论(0) 推荐(0) 编辑

Edit Distance

摘要: Given two wordsword1andword2, find the minimum number of steps required to convertword1toword2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) Insert a characterb) Delete a characterc) Replace a character思路:动规代码: 1 int min(int a, int b){ 2 ... 阅读全文

posted @ 2013-11-03 20:52 waruzhi 阅读(148) 评论(0) 推荐(0) 编辑

Linked List Cycle II

摘要: 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 space?思路:在上一题的基础上,假设head到环入口的距离为a,相遇点到环入口的距离为b,换的周长为r,则第一次相遇时有2*(a+b) = a + b + nra = nr - b也就是说如果从相遇点和head处同时出发两个指针,步数一样,那么就会在环入口相遇,据此可以获得入口的指针代码 ListNode ... 阅读全文

posted @ 2013-11-03 10:05 waruzhi 阅读(150) 评论(0) 推荐(0) 编辑

Linked List Cycle

摘要: Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?思路:快慢指针。两个指针同时从head出发,一个每次加一,一个每次加二,如果相遇的话就存在环。代码 1 bool hasCycle(ListNode *head) { 2 // IMPORTANT: Please reset any member data you declared, as 3 // the same Solution ins... 阅读全文

posted @ 2013-11-03 10:02 waruzhi 阅读(110) 评论(0) 推荐(0) 编辑

Next Permutation

摘要: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).The replacement must be in-place, do not allocate extra memory.He 阅读全文

posted @ 2013-11-03 09:07 waruzhi 阅读(245) 评论(0) 推荐(0) 编辑

导航