上一页 1 ··· 3 4 5 6 7 8 9 10 11 ··· 17 下一页

2013年11月9日

Reverse Linked List II

摘要: Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.Note:Givenm,nsatisfy the following condition:1 ≤m≤n≤ length of list.思路:用四个指针分别记录第m-1,m, n, n+1个节点,然后把第m到n个节点之间的node的next指向 阅读全文

posted @ 2013-11-09 20:11 waruzhi 阅读(159) 评论(0) 推荐(0) 编辑

Partition List

摘要: Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given1->4->3->2->5->2andx= 3,return1->2->2->4->3- 阅读全文

posted @ 2013-11-09 17:42 waruzhi 阅读(165) 评论(0) 推荐(0) 编辑

Valid Sudoku

摘要: Determine if a Sudoku is valid, according to:Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character'.'.A partially filled sudoku which is valid.思路:思路很简单,只要检查每行、每列、每个子区域中没有重复的数字即可。代码: 1 bool isValidSudoku(vector > &board) 阅读全文

posted @ 2013-11-09 13:09 waruzhi 阅读(172) 评论(0) 推荐(0) 编辑

Trapping Rain Water

摘要: Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example,Given[0,1,0,2,1,0,1,3,2,1,2,1], return6.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of 阅读全文

posted @ 2013-11-09 09:05 waruzhi 阅读(261) 评论(0) 推荐(0) 编辑

2013年11月8日

Construct Binary Tree from Preorder and Inorder Traversal

摘要: Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.代码: 1 TreeNode *buildTree(vector &preorder, vector &inorder) { 2 // IMPORTANT: Please reset any member data you declared, as 3 // the same Soluti... 阅读全文

posted @ 2013-11-08 21:54 waruzhi 阅读(116) 评论(0) 推荐(0) 编辑

Flatten Binary Tree to Linked List

摘要: Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6代码: 1 void flatten(TreeNod... 阅读全文

posted @ 2013-11-08 21:27 waruzhi 阅读(172) 评论(0) 推荐(0) 编辑

Permutations II

摘要: Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2]have the following unique permutations:[1,1,2],[1,2,1], and[2,1,1].思路:同Permutations。代码: 1 vector > permuteUnique(vector &num) { 2 // IMPORTANT: Please reset any member ... 阅读全文

posted @ 2013-11-08 21:11 waruzhi 阅读(152) 评论(0) 推荐(0) 编辑

Unique Paths II

摘要: Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as1and0respectively in the grid.For example,There is one obstacle in the middle of a 3x3 grid as illustrated below.[ [0,0,0], [0,1, 阅读全文

posted @ 2013-11-08 20:48 waruzhi 阅读(138) 评论(0) 推荐(0) 编辑

Path Sum II

摘要: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ [5,4,11,2]... 阅读全文

posted @ 2013-11-08 20:32 waruzhi 阅读(128) 评论(0) 推荐(0) 编辑

Unique Binary Search Trees II

摘要: Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.For example,Givenn= 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ... 阅读全文

posted @ 2013-11-08 20:18 waruzhi 阅读(139) 评论(0) 推荐(0) 编辑

上一页 1 ··· 3 4 5 6 7 8 9 10 11 ··· 17 下一页

导航