摘要: 题目:Implement strStr().Returns a pointer to the first occurrence of needle in haystack, ornullif needle is not part of haystack.思路: 指针的操作,大字符串从头到尾走,遇到与小字符串相同的就一起走,不同的就只走大字符串。小字符串走完的时候返回结果。代码: 1 class Solution { 2 public: 3 char *strStr(char *haystack, char *needle) { 4 5 ... 阅读全文
posted @ 2013-05-07 12:01 tanghulu321 阅读(131) 评论(0) 推荐(0) 编辑
摘要: 题目:Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"思路: 可以用递归一步步构造字符串。当左括号出现次数<n时,就可以放置新的左括号。当右括号出现次数小于左括 阅读全文
posted @ 2013-05-07 09:47 tanghulu321 阅读(126) 评论(0) 推荐(0) 编辑
摘要: 题目: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思路:此题为前序遍历。 可以用迭代也可以用stack。... 阅读全文
posted @ 2013-05-07 02:17 tanghulu321 阅读(174) 评论(0) 推荐(0) 编辑
摘要: 题目:Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space.思路:主要的思想就是把对应的数放到对应的索引上,例如1放到A[1]上,这样只需要O(n)的遍历就能完成,然后用一个O(n)的遍历找第一个没有放到索引上的数返回。代码: 1 class Solution { 2 pub. 阅读全文
posted @ 2013-05-07 01:09 tanghulu321 阅读(132) 评论(0) 推荐(0) 编辑
摘要: 题目: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'.'.思路: 做三个判断: 行, 列, block代码: 1 class Solution { 2 public: 3 bool isValidSudoku(vector<vector<char> > &board) 阅读全文
posted @ 2013-05-07 00:24 tanghulu321 阅读(123) 评论(0) 推荐(0) 编辑