代码改变世界

[LeetCode]Binary Tree Postorder Traversal

2014-03-14 16:14 by 庸男勿扰, 117 阅读, 0 推荐, 收藏, 编辑
摘要:原题链接:http://oj.leetcode.com/problems/binary-tree-postorder-traversal/题意描述:Given a binary tree, return thepostordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[3,2,1].Note:Recursive solution is trivial, could you do it iteratively?题解: 二叉树的后序遍历,... 阅读全文

[LeetCode]Minimum Path Sum

2014-03-14 16:06 by 庸男勿扰, 134 阅读, 0 推荐, 收藏, 编辑
摘要:原题链接:http://oj.leetcode.com/problems/minimum-path-sum/题意描述:Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right at any point in time.题解: 这是提到基本的动态规划题,转移方程为:dp[i][j] . 阅读全文

[LeetCode]Search Insert Position

2014-03-14 11:13 by 庸男勿扰, 127 阅读, 0 推荐, 收藏, 编辑
摘要:原题链接:http://oj.leetcode.com/problems/search-insert-position/题意描述:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here are few examples.[1,3,5,6], 5 → 2[ 阅读全文

[LeetCode]Linked List Cycle

2014-03-14 11:07 by 庸男勿扰, 253 阅读, 0 推荐, 收藏, 编辑
摘要:原题链接:http://oj.leetcode.com/problems/linked-list-cycle/题意描述:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?题解: 判断一个单链表中是否有环,这是一道典型的快慢指针的题,具体概念参见百度:http://baike.baidu.com/link?url=6vrn7yMJjWonSHrzGNLZA16JoJZxrLkFuRtJH3oP3PHCBakSKKM2UQH-5xt. 阅读全文

[LeetCode]Binary Tree Preorder Traversal

2014-03-14 00:20 by 庸男勿扰, 182 阅读, 0 推荐, 收藏, 编辑
摘要:原题链接:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/题意描述:Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Note:Recursive solution is trivial, could you do it iteratively?题解: 裸的二叉树遍历,没什么... 阅读全文

[LeetCode]Longest Common Prefix

2014-03-11 20:04 by 庸男勿扰, 125 阅读, 0 推荐, 收藏, 编辑
摘要:原题链接:http://oj.leetcode.com/problems/longest-common-prefix/题目描述:Write a function to find the longest common prefix string amongst an array of strings.题解: 依然是一道分治法解的题,类似的还有http://www.cnblogs.com/codershell/p/3592992.html 1 class Solution { 2 public: 3 string lcp(string str1,string str2){ 4 ... 阅读全文

[LeetCode]Merge k Sorted Lists

2014-03-10 23:42 by 庸男勿扰, 348 阅读, 0 推荐, 收藏, 编辑
摘要:原题链接:http://oj.leetcode.com/problems/merge-k-sorted-lists/题目描述:Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.题解: 这是个典型的分治题,用递归,不断平分,解决merge两个有序链表的小问题即可。 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ... 阅读全文

[LeetCode]Reverse Words in a String

2014-03-09 19:02 by 庸男勿扰, 308 阅读, 0 推荐, 收藏, 编辑
摘要:原题链接:http://oj.leetcode.com/problems/reverse-words-in-a-string/题目描述:Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Clarification:What constitutes a word?A sequence of non-space ch 阅读全文

[GC]一个简单的Garbage Collector的实现

2013-12-23 00:52 by 庸男勿扰, 2194 阅读, 10 推荐, 收藏, 编辑
摘要:前言: 最近看了google的工程师写的一个非常简单的垃圾收集器,大概200多行C代码,感叹大牛总能够把复杂的东西通过很简单的语言和代码表达出来。为了增加自己的理解,决定把大牛的想法和代码分析一遍,与大家分享,顺便结合wikipedia,复习下GC的基本概念。 相信大家在写程序的过程中都遇到内存管理的问题,诸如malloc/delete、new/free等,C/C++需要程序员主动进行内存的释放,即垃圾内存的回收,而像Java就提供了GC机制来自动进行垃圾回收。一、垃圾与垃圾回收 为什么需要进行垃圾回收呢? 垃圾回收就是要让程序员感觉有“无限”的内存供他一直allocate,事实上... 阅读全文

[LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)

2013-12-21 00:21 by 庸男勿扰, 330 阅读, 0 推荐, 收藏, 编辑
摘要:原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/题目描述:Evaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another expression.Some examples: ["2", "1", "+", "3&qu 阅读全文