摘要: 题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space?解题思路: 使用快慢指针,快指针每次走两步,慢指针每次走一步... 阅读全文
posted @ 2014-05-13 20:42 ThreeMonkey 阅读(80) 评论(0) 推荐(0) 编辑
摘要: 题目: Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up: Can you solve it without using extr... 阅读全文
posted @ 2014-05-13 20:40 ThreeMonkey 阅读(95) 评论(0) 推荐(0) 编辑
摘要: 题目: Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. ... 阅读全文
posted @ 2014-05-13 20:12 ThreeMonkey 阅读(196) 评论(0) 推荐(0) 编辑
摘要: 题目: Given a binary tree, return thepreordertraversal of its nodes' values. For example: Given binary tree{1,#,2,3}, 1 \ 2 / ... 阅读全文
posted @ 2014-05-13 20:09 ThreeMonkey 阅读(91) 评论(0) 推荐(0) 编辑
摘要: 题目: Given a binary tree, return thepostordertraversal of its nodes' values. For example: Given binary tree{1,#,2,3}, 1 \ 2 / ... 阅读全文
posted @ 2014-05-13 20:04 ThreeMonkey 阅读(118) 评论(0) 推荐(0) 编辑
摘要: 题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset. get(key)- Get ... 阅读全文
posted @ 2014-05-13 19:55 ThreeMonkey 阅读(146) 评论(0) 推荐(0) 编辑
摘要: 题目: Sort a linked list using insertion sort.解题思路: 假设 list[1..i]是排好序的,找到第i+1个元素应该插入的位置及其前驱,然后将其插入。代码: /** * Definition for singly-linked list. * str... 阅读全文
posted @ 2014-05-13 19:46 ThreeMonkey 阅读(121) 评论(0) 推荐(0) 编辑
摘要: 题目: Sort a linked list inO(nlogn) time using constant space complexity.解题思路: 复杂度为O(n* logn) 的排序算法有:快速排序、堆排序、归并排序。对于链表这种数据结构,使用归并排序比较靠谱。递归代码如下:代码: /... 阅读全文
posted @ 2014-05-13 19:41 ThreeMonkey 阅读(149) 评论(0) 推荐(0) 编辑
摘要: 题目: Givennpoints on a 2D plane, find the maximum number of points that lie on the same straight line.解题思路: 第一反应:枚举两个点组成的直线,然后看其他的点在不在这条直线上,在此过程中统计最大... 阅读全文
posted @ 2014-05-13 19:37 ThreeMonkey 阅读(140) 评论(0) 推荐(0) 编辑
摘要: 题目: Evaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another e... 阅读全文
posted @ 2014-05-13 19:28 ThreeMonkey 阅读(125) 评论(0) 推荐(0) 编辑
摘要: 题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the".解题思路: 1、先对字符串进行一次总... 阅读全文
posted @ 2014-05-13 19:16 ThreeMonkey 阅读(130) 评论(0) 推荐(0) 编辑