|
|
|
|
|
|
[LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
摘要:105. Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may
阅读全文
[LeetCode]Copy List with Random Pointer
摘要:题目:Copy List with Random Pointer A linked list is given such that each node contains an additional random pointer which could point to any node in the
阅读全文
[LeetCode]Top K Frequent Elements
摘要:题目:Top K Frequent Elements Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return
阅读全文
[LeetCode]Integer Break
摘要:题目:Integer Break Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Retur
阅读全文
[LeetCode]Gray Code
摘要:题目:Gray Code 给定格雷码的位数,输出所有的格雷码对应的十进制数。 思路: 000 0 001 1 011 3 010 2 110 6 111 7 101 5 100 4 前四个和后四个的前两位对称,前两个和后两个的第一位是对称的。 如此,可看出,格雷码有一定的对称性。
阅读全文
[LeetCode]Increasing Triplet Subsequence
摘要:题目:Increasing Triplet Subsequence Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally th
阅读全文
[LeetCode]Odd Even Linked List
摘要:题目:Odd Even Linked List Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the
阅读全文
[LeetCode]Wiggle Sort
摘要:题目:Wiggle Sort II Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... Example:(1) Given nums = [1, 5, 1, 1, 6
阅读全文
[LeetCode]Range Sum Query
摘要:题目:Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Note: 给定
阅读全文
[LeetCode]Perfect Squares
摘要:题目:Perfect Squares 给定一个正整数n,找到总和为n的最小数量的完美平方数(例如,1,4,9,16,...)。 For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 bec
阅读全文
[LeetCode]Basic Calculator
摘要:题目:Basic Calculator 给定一个合法的运算表达式,该表达式中只包含数字、'+'、'-'、' '、'('、')'。 思路: 简单思考不用看成加减两种运算,直接看成加法,只不过由正负; 如何处理括号呢?因为只看成加法,括号会影响的是数值的正负,那么通过去括号运算法则来修改当前值的正负就可
阅读全文
[LeetCode]Contains Duplicate
摘要:题目:Contains Duplicate 给定整数数组,查找数组是否包含任何重复项。 如果数组中的任何值至少出现两次,则函数应返回true,如果每个元素都不同,则返回false。 思路: 可以使用桶排序的思想来,遍历所有元素,通过元素值直接访问标志,当第一次遇到则设标志,第二次遇到就跳出循环。 但
阅读全文
[LeetCode]Shortest Palindrome
摘要:题目:Shortest Palindrome 给定字符串,在前面增加最少字符使其组成回文字符串。 思想: 只要找到从字符串头部开始的最长回文子串,就能将剩下的字符串逆置后拼接到前面而得到最短的回文字符串。 /**判断字符串是回文的**/ bool isPalindrome(string s){ if
阅读全文
[LeetCode]House Robber
摘要:题目:House Robber 给定一个数组求最大和,要求求和的每个元素不能相邻。 思路: 不要想太复杂,每次记录当前的最大值和前一个位置的最大值,然后比较即可。 具体就是动态规划的思想: F(i)表示数组前i项中符合上面要求的最大和;choose(i) = true表示F(i)的和中第i项被选中了
阅读全文
[LeetCode]Course Schedule
摘要:题目:Course Schedule 给定了n各课程[0,n-1]和课程之间的依赖关系,课程i必须先完成课程j,即:课程i依赖于课程j。判断这些课程能否修完。 思路: 这些课程学习过程类似于拓扑排序,终点是要判断课程学习顺序(依赖关系)中是否有环,如果有环,则不能修完全部课程。 课程可以看成点,课程
阅读全文
[LeetCode]Isomorphic Strings
摘要:题目:Isomorphic Strings 给定两个字符串(长度相等),判断它们是否同构;同构:两个字符串的字母对应位置是否能够一一对应。 For example,Given "egg", "add", return true. Given "foo", "bar", return false. G
阅读全文
[LeetCode]Ugly Number
摘要:题目:Ugly Number 正数中质因子只有2,3,5的数被称为"丑数"。判断一个数是不是"丑数"。 例如前十个最小的丑数:1 2 3 4 5 6 8 9 10 12 思路: 合数能表示成质数的积的形式,所以,丑数必然是只有2,3,5的因子。 题目:Ugly Number II 找到第n个丑数。
阅读全文
[LeetCode]Surrounded Regions
摘要:题目:Surrounded Regions 给定一个二维矩阵由'x'和'o'表示,其中o被x包围了,如果o上下左右中有其他的o,则被看做是连在一起的,而一起的o中有一个贴边了,就表示这些o都是活的。死的o要替换成x。 思路: 采用广度优先搜索或深度优先搜索来找到连在一起的o;只要遍历过程中有一个贴边
阅读全文
[LeetCode]Best Time to Buy and Sell Stock
摘要:题目:Best Time to Buy and Sell Stock 给定一个数组,数组中一个元素表示一天的股价,求一次交易能得到的最大收益。 思路: 数组可能是多个升序降序组成,只要能找到一组极值,使它们的差最大就可以了。 这样实际上就是每当找到一个极大值,就判断此时的到的差值是否比记录的最大值大
阅读全文
[LeetCode]Repeated DNA Sequences
摘要:题目:Repeated DNA Sequences 给定包含A、C、G、T四个字符的字符串找出其中十个字符的重复子串。 思路: 首先,string中只有ACGT四个字符,因此可以将string看成是1,3,7,20这三个数字的组合串; 并且可以发现{ACGT}%5={1,3,2,0};于是可以用两个
阅读全文
|
|