随笔分类 - Interview Algorithm
摘要:1. Convert Expression to Reverse Polish Notation http://www.lintcode.com/en/problem/convert-expression-to-polish-notation/ Given an expression string array, return the Polish notation of this e...
阅读全文
摘要:1 Same Tree https://leetcode.com/problems/same-tree/ Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurall...
阅读全文
摘要:前段时间在看一本01年出的旧书《effective Tcp/Ip programming》,这个算法专题中断了几天,现在继续写下去。 Introduction 对于单向链表(singly linked list),每个节点有⼀个next指针指向后一个节点,还有一个成员变量用以储存数值;对于双向链表(Doubly LinkedList),还有一个prev指针指向前一个节点。与数组类似,搜索链表需...
阅读全文
摘要:BST 以下BST的定义来自于Wikipedia: Binary Search Tree, is a node-based binary tree data structure which has the following properties: The left subtree of a node contains only nodes with keys less ...
阅读全文
摘要:1. Palindrome Partitioning https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every substring of the partition is a palindrome. Return all possibl...
阅读全文
摘要:基础知识 二分非递归写法: int binary_search(const int a[], const int size, const int val) { int lower = 0; int upper = size-1; /* invariant: if a[i]==val for any i, then lower >1; if (val ==...
阅读全文
摘要:给定一个等概率随机产生1~M的随机函数rand1ToM如下: public int rand1ToM(int m) { return (int) (Math.random() * m) + 1; } 除此之外不能使用任何额外的随机机制。有两个输入参数分别为m和n,请用rand1ToM(m)实现等概率随机产生1~n的随机函数rand1ToN。 Solution 在做这道题之前,我们先...
阅读全文
摘要:https://leetcode.com/problems/kth-largest-element-in-an-array/ Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinc...
阅读全文
摘要:首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度。子序列需要以相关的顺序呈现,但不必连续。例如,“abc”, “abg”, “bdf”, “aeg”, ‘”acefg”等都是“abcdefg”的子序列。因此,一个长度为n的序列拥有2^n中可能的子序列(序列中的每一个元素只有选或者不选两种可能,因此是2^n)。 Example: LCS for input ...
阅读全文
摘要:在 Dynamic Programming | Set 1 (Overlapping Subproblems Property) 和 Dynamic Programming | Set 2 (Optimal Substructure Property) 中我们已经讨论了重叠子问题和最优子结构性质,现在我们来看一个可以使用动态规划来解决的问题:最长上升子序列(Longest Increasing S...
阅读全文
摘要:正如我们在 Dynamic Programming | Set 1 (Overlapping Subproblems Property) 中讨论的那样,当一个问题具有以下2种性质时,建议使用动态规划来求解: 1 重叠子问题(Overlapping Subproblems) 2 最优子结构(Optimal Substructure) 我们已经在 Dynamic Programming | Se...
阅读全文
摘要:动态规划是这样一种算法范式:将复杂问题划分为子问题来求解,并且将子问题的结果保存下来以避免重复计算。如果一个问题拥有以下两种性质,则建议使用动态规划来求解。 1 重叠子问题(Overlapping Subproblems) 2 最优子结构(Optimal Substructure) 1 重叠子问题 类似于分治法,动态规划将子问题的解合并。当多次需要用到子问题的解时,应当考虑使用动态规划。在...
阅读全文
摘要:http://www.lintcode.com/en/problem/remove-node-in-binary-search-tree/#Given a root ofBINARY Search Tree with unique value for each node. Remove the no...
阅读全文
摘要:http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence-ii/# Give you an integer matrix (with row size n, column size m),find the longest increasing continuous subsequence i...
阅读全文
摘要:http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/# Give you an integer array (index from 0 to n-1, where n is the size of this array),find the longest increasing cont...
阅读全文
摘要:Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], an...
阅读全文
摘要:http://www.lintcode.com/en/problem/permutations/# Given a list of numbers, return all possible permutations. Example For nums = [1,2,3], the permutations are: [ [1,2,3], [1,3,2], ...
阅读全文
摘要:http://www.lintcode.com/zh-cn/problem/topological-sorting/# 给定一个有向图,图节点的拓扑排序被定义为: 对于每条有向边A--> B,则A必须排在B之前 拓扑排序的第一个节点可以是任何在图中没有其他节点指向它的节点 找到给定图的任一拓扑排序 solution Topologi...
阅读全文
摘要:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity?...
阅读全文
摘要:https://leetcode.com/problems/search-in-rotated-sorted-array/ Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Y...
阅读全文

浙公网安备 33010602011771号