上一页 1 2 3 4 5 6 ··· 8 下一页
摘要: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.Solution: Uses map to recorde node mapping between old linked list and new linked list. 1 RandomListNode *copyRandomList(RandomListNod... 阅读全文
posted @ 2013-11-27 12:45 假日笛声 阅读(165) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keysless thanthe node's key.The right subtree of a node contains only nodes with keysgreater thanthe node's key.Both the left and ri 阅读全文
posted @ 2013-11-26 14:28 假日笛声 阅读(218) 评论(0) 推荐(0) 编辑
摘要: You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?Solution: 1 void rotate(vector > &matrix) { 2 int n = matrix.size(); 3 for(int i = 0; i < n / 2; i ++) { 4 for(int j = i; j < n - 1 - i; ... 阅读全文
posted @ 2013-11-26 13:25 假日笛声 阅读(527) 评论(0) 推荐(0) 编辑
摘要: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input:(2 -> 4 -> 3) + (5 -> 6 -> 4)Output:7 -> 0 -> 8Summary: Be careful a 阅读全文
posted @ 2013-11-25 15:51 假日笛声 阅读(170) 评论(0) 推荐(0) 编辑
摘要: Implement strStr().Returns a pointer to the first occurrence of needle in haystack, ornullif needle is not part of haystack.Summary: be careful about the corner case, haystack = "", needle = "" 1 char *strStr(char *haystack, char *needle) { 2 if(haystack[0] == '\0' && 阅读全文
posted @ 2013-11-23 15:34 假日笛声 阅读(163) 评论(0) 推荐(0) 编辑
摘要: Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?Sulotion: Considers a BST as a storted array, so we can traverse the BST inorderly 阅读全文
posted @ 2013-11-21 15:33 假日笛声 阅读(201) 评论(0) 推荐(0) 编辑
摘要: Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right at any point in time.Summary: DP, calucate the minimun sum from start point to every cell unitil we traverse ever 阅读全文
posted @ 2013-11-21 15:08 假日笛声 阅读(422) 评论(0) 推荐(0) 编辑
摘要: Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.Solution: Just do it recursively. 1 TreeNode *build(vector &preorder, int pstart, int pend, vector &inorder, int istart, int iend) { 2 TreeNode * root = ... 阅读全文
posted @ 2013-11-21 12:09 假日笛声 阅读(185) 评论(0) 推荐(0) 编辑
摘要: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value)- Set or insert the value if the key is not 阅读全文
posted @ 2013-11-18 06:21 假日笛声 阅读(544) 评论(0) 推荐(0) 编辑
摘要: Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0Summary: basic bin 阅读全文
posted @ 2013-11-15 14:17 假日笛声 阅读(169) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 ··· 8 下一页