LeetCode
文章平均质量分 62
monkeyduck
清华大学CS在读,研究领域为语音处理、模式识别、对话管理,依然在不断学习中,成功在于点滴积累!
展开
-
【LeetCode】290. Word Pattern
题目Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.Examples:原创 2016-05-14 10:52:03 · 515 阅读 · 0 评论 -
【LeetCode】115. Distinct Subsequences
题目Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none)原创 2016-05-17 21:16:08 · 603 阅读 · 0 评论 -
【LeetCode】92. Reverse Linked List II
题目Reverse a linked list from position m to n. Do it in-place and in one-pass.For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note: Given m, n satisfy the following原创 2016-05-22 13:32:32 · 531 阅读 · 0 评论 -
【LeetCode】50. Pow(x, n)
题目Implement pow(x, n).Subscribe to see which companies asked this question思路两种方法,一:一个一个乘,最多乘INT_MAX次,显然不太好。 二:每次求n/2n/2次幂,最多32次陷阱此题陷阱非常多,主要需要考虑几种特殊情况: 1. n 为 0:结果为1 2. n 为负数,需要先求再求倒数,或者直接求1/x1/x的-n次原创 2016-05-22 14:33:53 · 550 阅读 · 0 评论 -
【LeetCode】310. Minimum Height Trees
题目链接思路我想到了n个点构成树,那么一定会有n-1条边,构成的树深度最大的情况发生在所有点位于一条链上,此时深度为 n/2n/2 或 n/2+1n/2 + 1 ,这取决于n的奇偶性。树深度最浅的情况发生在一个根节点,所有其它节点都与根节点直接相连,此时深度为2。我还发现一个规律,就是叶子节点的数目+树的最长路径为一定值,为节点数+2。这样统计出有多少个叶子节点后,再走最长路径的一半所到达的节点就是原创 2016-06-04 14:29:09 · 1028 阅读 · 0 评论 -
【LeetCode】40. Combination Sum II
题目Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combination.原创 2016-05-22 22:37:24 · 899 阅读 · 0 评论 -
【LeetCode】31. Next Permutation
题目思路找到下一个排列,比较好的思路是 O(n)O(n) ,从尾向前遍历,找到第一个nums[i-1]代码void nextPermutation(vector<int>& nums) { int N = nums.size(); bool flag = false; int i,j; for (i=N-1;i>=1;--i){原创 2016-06-05 12:19:03 · 578 阅读 · 0 评论 -
先序遍历中序遍历后序遍历确定一棵二叉树(递归、非递归)
先序遍历和中序遍历确定二叉树后序遍历和中序遍历确定二叉树原创 2016-09-16 14:04:46 · 1604 阅读 · 1 评论 -
[LeetCode]Basic Calculator II
题目不难,但是想做对却也需要考虑很多细节。 没有括号的运算就是先乘除后加减,思路是把➕和➖看作分隔符,先计算分隔符隔开的一个个term,再把term相加。 举个例子:a+b*c/d-e,在这个例子中,读到第一个➕时,term是a,并且我们可以认为a已经被➕分割开了,可以加到总和里去了,所以sum现在是a,term往后读一个数,是b。然后往后读一个符号是✖️,所以term=term*c,即term原创 2016-10-11 12:05:44 · 960 阅读 · 0 评论 -
【LeetCode】55. Jump Game
题目Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you a原创 2016-05-22 10:48:55 · 759 阅读 · 0 评论 -
【LeetCode】264. Ugly Number II
题目Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ug原创 2016-05-21 15:06:58 · 699 阅读 · 0 评论 -
【LeetCode】234. Palindrome Linked List
题目Given a singly linked list, determine if it is a palindrome.Follow up: Could you do it in O(n) time and O(1) space?Subscribe to see which companies asked this question思路我的思路想了半天没有想出空间为1的解法,就用了最先考虑到的原创 2016-05-21 11:34:47 · 493 阅读 · 0 评论 -
【LeetCode】330. 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] inclusive can be formed by the sum of some elements in the array. Ret原创 2016-05-14 11:59:36 · 710 阅读 · 0 评论 -
【LeetCode】16. 3Sum Closest
题目Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactl原创 2016-05-14 16:30:21 · 434 阅读 · 0 评论 -
【LeetCode】95. Unique Binary Search Trees II
题目Given n, generate all structurally unique BST’s (binary search trees) that store values 1…n.For example, Given n = 3, your program should return all 5 unique BST’s shown below.思路此题最好想的思路是递归,当确定根节点是原创 2016-05-15 17:11:24 · 659 阅读 · 0 评论 -
【LeetCode】134. Gas Station
题目There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its n原创 2016-05-29 23:36:36 · 2146 阅读 · 0 评论 -
【LeetCode】207. Course Schedule
题目There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pai原创 2016-05-30 23:40:09 · 1790 阅读 · 0 评论 -
【LeetCode】209. Minimum Size Subarray Sum
题目Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn’t one, return 0 instead.For example, given the array [2,3,1,2,原创 2016-05-31 19:45:30 · 650 阅读 · 0 评论 -
【LeetCode】82. Remove Duplicates from Sorted List II
思路很简单的一道题,思路也没什么特别的,就是设置一个prehead节点代码 ListNode* deleteDuplicates(ListNode* head) { ListNode nHead(0); ListNode* phead = &nHead; ListNode* tmp = &nHead; bool flag;原创 2016-05-31 20:19:58 · 535 阅读 · 0 评论 -
【LeetCode】72. Edit Distance
题目Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a)原创 2016-05-21 10:51:23 · 592 阅读 · 0 评论 -
LeetCode总结
终于把LeetCode免费题目都做完了,接下来要做第二遍,想把第一遍不会做的题目整理一下思路,为接下来的google面试准备一下,万一过了呢,哈哈。315 Count of Smaller Numbers After Self 题意:计算每个元素右面有多少个元素小于它 从后向前遍历,维护一个排好序的数组,每次插入新元素时确定其在有序数组的位置,就是要求的右面有多少个元素小原创 2016-09-02 10:26:04 · 1481 阅读 · 1 评论