[LeetCode]Palindrome Pairs
摘要:题目:Palindrome Pairs Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two wo
阅读全文
[LeetCode]The Skyline Problem
摘要:题目:The Skyline Problem A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. N
阅读全文
[LeetCode]Implement Trie (Prefix Tree)
摘要:题目:Implement Trie (Prefix Tree) 实现字典树。 什么是字典树? Trie树,又叫字典树、前缀树(Prefix Tree)、单词查找树 或 键树,是一种多叉树结构。如下图: 上图是一棵Trie树,表示了关键字集合{“a”, “to”, “tea”, “ted”, “ten
阅读全文
[LeetCode]Self Crossing
摘要:题目:Self Crossing You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the
阅读全文
[LeetCode]Patching Array
摘要:题目:Patching Array Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n]inc
阅读全文
[LeetCode]Longest Increasing Path in a Matrix
摘要:题目:Longest Increasing Path in a Matrix Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to
阅读全文
[LeetCode]Count of Range Sum
摘要:题目:Count of Range Sum Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Range sum S(i, j) is defined a
阅读全文
[LeetCode]Integer to English Words
摘要:题目:Integer to English Words Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. For
阅读全文
[LeetCode]Sliding Window Maximum
摘要:题目:Sliding Window Maximum 给定一个数组和滑动窗口的大小,窗口从开始位置每次向后滑动一格,找出每次窗口的最大值; 思路: 记录当前窗口的最大值下标,每次移动一格时,最小下标会移出窗口,邻接着的最大下标会移入窗口; 因此,每次窗口移动时,首先比较移入的是否比当前最大值还大,是则
阅读全文
[LeetCode]Basic Calculator
摘要:题目:Basic Calculator 给定一个合法的运算表达式,该表达式中只包含数字、'+'、'-'、' '、'('、')'。 思路: 简单思考不用看成加减两种运算,直接看成加法,只不过由正负; 如何处理括号呢?因为只看成加法,括号会影响的是数值的正负,那么通过去括号运算法则来修改当前值的正负就可
阅读全文
[LeetCode]Shortest Palindrome
摘要:题目:Shortest Palindrome 给定字符串,在前面增加最少字符使其组成回文字符串。 思想: 只要找到从字符串头部开始的最长回文子串,就能将剩下的字符串逆置后拼接到前面而得到最短的回文字符串。 /**判断字符串是回文的**/ bool isPalindrome(string s){ if
阅读全文
[LeetCode]Best Time to Buy and Sell Stock
摘要:题目:Best Time to Buy and Sell Stock 给定一个数组,数组中一个元素表示一天的股价,求一次交易能得到的最大收益。 思路: 数组可能是多个升序降序组成,只要能找到一组极值,使它们的差最大就可以了。 这样实际上就是每当找到一个极大值,就判断此时的到的差值是否比记录的最大值大
阅读全文
[LeetCode]Max Points on a Line
摘要:题目:Max Points on a Line 找到给定的点集中在一条直线上的最大点数。 思路: 两点确定一条直线,可以通过y=k*x+b中的k和b来确定直线; k=(y1-y0)/(x1-x0) b=(x1*y0-x0*y1)/(x1-x0); 所以用double类型存储k,b; 遍历每个点找到k
阅读全文
[LeetCode]LRU Cache
摘要:题目:LRU Cache 操作系统中页面置换算法中有LRU算法(最近最久未用算法),其算法原理如下: 每个页面调入内存时,会有一个记录当前页面距离最近一次访问的时间间隔。 当每次访问页面时,如果页面已经在内存中,就将该页面对应的时间标志清空,其他的标志加一; 如果当前页面不在内存中且内存中没有空闲的
阅读全文
[LeetCode]Word Break
摘要:题目:Word Break 将给定的字符串按照给定的单词表拆分开,判断是否能在给定的单词表中拆分该字符串 单词表中单词不重复,但是可以在字符串中重复出现。 思路: 暴力搜索,只要遍历单词表每次找到与字符串的当前位置往后的n(单词表中当前遍历到的单词长度)的字符重合则表示可以拆分。 这种方法,时间复杂
阅读全文
[LeetCode]Container With Most Water
摘要:题目:Container With Most Water 给定一个int数组,找到两个值,以其下标的差为宽度,最短高度为高度求面积。 思路: 定义两个指针,一个从前往后,一个从后往前; 每次固定前面的指针,后面的遍历一遍找到最大的面积(和前面的循环结果比较)记录下来。 还有复杂度O(n)的做法。 同
阅读全文