2013年10月10日

Longest Common Prefix

摘要: Write a function to find the longest common prefix string amongst an array of strings.思路:先求前两个字符串的公共前缀,然后拿这个前缀和其他字符串进行比对。 1 int min(int a, int b){ 2 if(a &strs) { 7 // Note: The Solution object is instantiated only once and is reused by each test case. 8 int n = strs.siz... 阅读全文

posted @ 2013-10-10 20:29 waruzhi 阅读(172) 评论(0) 推荐(0) 编辑

Palindrome Number

摘要: Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string, note the restriction of using extra space.You could also try reversing an integer. However, if you have solved 阅读全文

posted @ 2013-10-10 19:20 waruzhi 阅读(134) 评论(0) 推荐(0) 编辑

Container With Most Water

摘要: Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.Note: You 阅读全文

posted @ 2013-10-10 18:47 waruzhi 阅读(177) 评论(0) 推荐(0) 编辑

Single Number II

摘要: Given an array of integers, every element appearsthreetimes except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?思路是用一个32位的数result的每一位记录所有数在这一位出现的次数,如果是3的倍数则消去,最后留下来的数则是所要的结果。注意,result需要定义为unsigned int类型,否 阅读全文

posted @ 2013-10-10 18:22 waruzhi 阅读(267) 评论(0) 推荐(0) 编辑

Single Number

摘要: Given an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?进行一次扫描,把所有数据异或在一起,最终结果就是所求结果。1 int singleNumber(int A[], int n) {2 // Note: The Soluti... 阅读全文

posted @ 2013-10-10 17:55 waruzhi 阅读(135) 评论(0) 推荐(0) 编辑

2013年10月1日

Construct Binary Tree from Inorder and Postorder Traversal

摘要: 后序遍历的最后一个节点是根节点,在中序遍历中找到根节点所在的位置,将该vector分成左右两部分,然后在后序中找到和它们对应的部分,递归求出左右子树。 1 TreeNode *buildTree(vector &inorder, vector &postorder) { 2 // Start typing your C/C++ solution below 3 // DO NOT write int main() function 4 TreeNode *root = new TreeNode(0); 5 if(inorder.s... 阅读全文

posted @ 2013-10-01 21:49 waruzhi 阅读(281) 评论(0) 推荐(0) 编辑

Search in Rotated Sorted Array II

摘要: 在上题基础上,如果元素可以重复的话,那么就需要修改getPos,因为有可能直接得到的最小值不是正好在分界的地方。修改在第34行。 1 int findPos(int A[], int left, int right){ 2 if(left > right) 3 return -1; 4 int mid = (left+right)/2; 5 int result; 6 if(A[left] > A[mid]){ 7 result = findPos(A, left, mid-1); 8 if(result == -... 阅读全文

posted @ 2013-10-01 19:24 waruzhi 阅读(176) 评论(0) 推荐(0) 编辑

Search in Rotated Sorted Array

摘要: 首先利用二分查找的方法找到最小值,然后在以最小值为分界的两块分别进行二分搜索。 1 int findPos(int A[], int left, int right){ 2 if(left > right) 3 return -1; 4 int mid = (left+right)/2; 5 int result; 6 if(A[left] > A[mid]){ 7 result = findPos(A, left, mid-1); 8 if(result == -1) 9 return m... 阅读全文

posted @ 2013-10-01 19:11 waruzhi 阅读(200) 评论(0) 推荐(0) 编辑

2013年9月30日

Generate Parentheses

摘要: 递归。传入的前两个参数分别表示未分配的左括号数和未匹配的左括号数。 1 void fill(int numOfRemain, int numToMatch, string tmp, vector &result){ 2 if(numOfRemain == 0 && numToMatch == 0){ 3 result.push_back(tmp); 4 return; 5 } 6 if(numOfRemain > 0){ 7 tmp += '('; 8 ... 阅读全文

posted @ 2013-09-30 20:51 waruzhi 阅读(166) 评论(0) 推荐(0) 编辑

Path Sum

摘要: 1 bool hasPathSum(TreeNode *root, int sum) {2 // Start typing your C/C++ solution below3 // DO NOT write int main() function4 if(root == NULL)5 return false;6 if(sum == root->val && root->left==NULL && root->right==NULL)7 return true;8 ... 阅读全文

posted @ 2013-09-30 20:15 waruzhi 阅读(152) 评论(0) 推荐(0) 编辑

导航