摘要: Gray CodeThe gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integernrepresenting the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.For example, givenn= 2, return[0,1,3,2]. Its gr 阅读全文
posted @ 2014-04-04 01:42 flowerkzj 阅读(93) 评论(0) 推荐(0) 编辑
摘要: Generate ParenthesesGivennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"找出配对括号的所有组合,组合数就是卡特朗数,只是这里还要求组合,递归实现,关键 阅读全文
posted @ 2014-04-03 16:16 flowerkzj 阅读(121) 评论(0) 推荐(0) 编辑
摘要: Maximum SubarrayFind the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray[4,−1,2,1]has the largest sum =6.More practice:If you have figured out the O(n) solution, try coding anot 阅读全文
posted @ 2014-03-29 14:45 flowerkzj 阅读(188) 评论(0) 推荐(0) 编辑
摘要: 又是专题二重奏的节奏。这回的题目是word break,其实就是自然语言处理中的分词,不过说实话,从自然语言处理课学来的方法是前向最大匹配或者是后向最大匹配,又或者是两者结合啊,像这个貌似不一定按着语言规则走的,所以不能用自然语言处理中通过统计的方法去进行剪技,那怎么办呢?Word BreakGiven a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.For example, 阅读全文
posted @ 2014-03-28 15:29 flowerkzj 阅读(173) 评论(0) 推荐(0) 编辑
摘要: 二叉树遍历,各种非递归实现,非递归实现基本都是用到栈,然后关键要弄明白的是什么时候进栈,什么时候出栈,什么时候指针是指向左孩子,什么时候指向右孩子。先说先序遍历,先序遍历比较简单,从栈中拿出一个就打印一个,然后再把左右孩子进栈就行,要注意进栈是先右孩子,然后才是左孩子。看代码: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val... 阅读全文
posted @ 2014-03-25 14:14 flowerkzj 阅读(157) 评论(0) 推荐(0) 编辑
摘要: String to Integer (atoi)Implementatoito convert a string to an integer.Hint:Carefully consider all possible input cases. If you want a challenge, plea... 阅读全文
posted @ 2014-03-24 20:15 flowerkzj 阅读(165) 评论(0) 推荐(0) 编辑
摘要: Longest Substring Without Repeating CharactersGiven a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subs 阅读全文
posted @ 2014-03-24 19:46 flowerkzj 阅读(134) 评论(0) 推荐(0) 编辑
摘要: Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.大数相乘,就是模拟乘法的过程,注意好下标,别忘了最后检查高位的0就好。看代码: 1 class Solution { 2 public: 3 string multiply(string num1, string num2) { 4 ... 阅读全文
posted @ 2014-03-24 18:20 flowerkzj 阅读(144) 评论(0) 推荐(0) 编辑
摘要: 对排序数组中重复元素的删除操作,这里分两种情况,一种是重复的元素合并,另一种是重复元素的删除。先从简单的做起:合并Remove Duplicates from Sorted ListGiven a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3.这个就跟数组中重复元素合并差不多,只需要考虑 阅读全文
posted @ 2014-03-24 02:04 flowerkzj 阅读(181) 评论(0) 推荐(0) 编辑
摘要: 删除排序数组重复元素,先来个简单的。Remove Duplicates from Sorted ArrayGiven a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example,Given input array A =[1, 阅读全文
posted @ 2014-03-23 20:15 flowerkzj 阅读(141) 评论(0) 推荐(0) 编辑