随笔分类 -  leetcode

摘要:这个题目是典型的KMP算法,当然也可以试试BM,当然有关KMP和BM的介绍阮一峰曾经写过比较好的科普,然后july也有讲解,不过那个太长了。 先放题目吧: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of hays... 阅读全文
posted @ 2014-12-02 15:43 Tiny-Box 阅读(169) 评论(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 '.'. A... 阅读全文
posted @ 2014-12-02 15:19 Tiny-Box 阅读(168) 评论(0) 推荐(0)
摘要:这个还是一开始没读懂题目,题目如下:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or ... 阅读全文
posted @ 2014-11-26 00:04 Tiny-Box 阅读(184) 评论(0) 推荐(0)
摘要:这个题目很简单,给一个字符串,然后返回最后一个单词的长度就行。题目如下: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not ex... 阅读全文
posted @ 2014-11-25 23:55 Tiny-Box 阅读(137) 评论(0) 推荐(0)
摘要:这个非常简单的题目,题目如下: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 就是算导里面的第一个算法讲解,但是我的C++水平实在太渣,所以对于链表的操作很蠢,... 阅读全文
posted @ 2014-11-24 23:30 Tiny-Box 阅读(183) 评论(0) 推荐(0)
摘要:这个题目相对有点奇怪,题目如下: Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 可能是我思路的问题吧,我之前是在考虑先将string转成int或者double,然后将二进制转为十进制进行计算,最后再将这个十进制转为二进... 阅读全文
posted @ 2014-11-24 23:26 Tiny-Box 阅读(195) 评论(0) 推荐(0)
摘要:这道题其实让我意识到了我的英文水平还有待加强。。。。题目如下:Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that t... 阅读全文
posted @ 2014-11-23 22:59 Tiny-Box 阅读(171) 评论(0) 推荐(0)
摘要:这道题leetcode上面写着是DP问题,问题是我一开始写了个简单的递归结果直接超时,所以没办法只好拿迭代来做了。题目如下: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct w... 阅读全文
posted @ 2014-11-23 22:50 Tiny-Box 阅读(210) 评论(0) 推荐(0)
摘要:这个题目其实不难的,主要是我C++的水平太差了,链表那里绊了好久,但是又不像用python,所以还是强行上了。 题目如下: Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Give... 阅读全文
posted @ 2014-11-22 16:37 Tiny-Box 阅读(132) 评论(0) 推荐(0)
摘要:这道题的无聊之处在于题目其实给了一些很奇怪的测试用例。比如他会给一些空的数组来,但是这个是不科学的,因为在C++中不允许定义一个空的列表,我们用的又不是那种糙又快的python,所以在这里我遇到了一些问题,后来还是解决了。这道题题目如下:Given two sorted integer arrays... 阅读全文
posted @ 2014-11-20 22:56 Tiny-Box 阅读(127) 评论(0) 推荐(0)
摘要:因为我刷题是难度不是按发布日期,所以就有可能遇到这种情况,比如这个。。。Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal i... 阅读全文
posted @ 2014-11-18 13:40 Tiny-Box 阅读(116) 评论(0) 推荐(0)
摘要:这次我觉得我的智商太低,想了很久才写出来。题目是让求镜像二叉树判断,题目如下:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this... 阅读全文
posted @ 2014-11-18 12:57 Tiny-Box 阅读(155) 评论(0) 推荐(0)
摘要:跟第七题一样,把最后的输出顺序换一下就行。。。Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For exampl... 阅读全文
posted @ 2014-11-15 20:59 Tiny-Box 阅读(118) 评论(0) 推荐(0)
摘要:可能是因为我是按难度顺序刷的原因,这个其实在之前的几道题里面已经写过了。题目如下: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf nod... 阅读全文
posted @ 2014-11-15 20:53 Tiny-Box 阅读(100) 评论(0) 推荐(0)
摘要:这次相对来讲复杂点,题目如下: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,... 阅读全文
posted @ 2014-11-15 00:44 Tiny-Box 阅读(99) 评论(0) 推荐(0)
摘要:这个题目纠结了一会儿,终于从二叉树转化到AVL了。题目如下:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a ... 阅读全文
posted @ 2014-11-13 11:18 Tiny-Box 阅读(129) 评论(0) 推荐(0)
摘要:二叉树基本功练习题,题目如下:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to... 阅读全文
posted @ 2014-11-12 19:43 Tiny-Box 阅读(81) 评论(0) 推荐(0)
摘要:终于到了二叉树。题目如下:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the... 阅读全文
posted @ 2014-11-11 23:45 Tiny-Box 阅读(124) 评论(0) 推荐(0)
摘要:第三道还是帕斯卡三角,这个是要求正常输出,题目如下:Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5, Return[ [1], [1,1], ... 阅读全文
posted @ 2014-11-11 09:28 Tiny-Box 阅读(130) 评论(0) 推荐(0)
摘要:我是按难度往下刷的,第二道是帕斯卡三角形二。简单易懂,题目如下:Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3, Return [1,3,3,1].Note: Could y... 阅读全文
posted @ 2014-11-11 00:34 Tiny-Box 阅读(131) 评论(0) 推荐(0)