leetcode
文章平均质量分 70
gjwei1993
这个作者很懒,什么都没留下…
展开
-
Leetcode 543. Diameter of Binary Tree
Leetcode 543. Diameter of Binary TreeGiven a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nod原创 2017-06-16 20:53:24 · 147 阅读 · 0 评论 -
477. Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Now your job is to find the total Hamming distance between all pairs of the given numb原创 2017-06-22 10:19:46 · 229 阅读 · 0 评论 -
242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.For example, s = “anagram”, t = “nagaram”, return true. s = “rat”, t = “car”, return false.Note: You may assume the s原创 2017-06-22 10:24:29 · 133 阅读 · 0 评论 -
343. 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. Return the maximum product you can get.For example, given n = 2, return 1原创 2017-06-22 22:39:33 · 159 阅读 · 0 评论 -
454. 4Sum II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.To make problem a bit easier, all A, B, C, D have same length o原创 2017-06-24 09:35:34 · 204 阅读 · 0 评论 -
319. Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s off or turning off i原创 2017-06-24 10:01:47 · 144 阅读 · 0 评论 -
560. Subarray Sum Equals K
这道题目使用的是hash+prefix sum的方法,这个也是我第一次知道还有prefix sum这个东西 所谓的prefix sum可以看看链接https://www.jiuzhang.com/qa/1178/代码中,pre_sum[s - k]就是为了得到是否存在一个prefix sum使得s - p = k ,从而得到一个满足连续子序列的和为k。class Solution(object):原创 2017-07-14 08:43:11 · 223 阅读 · 0 评论 -
377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.Example:nums = [1, 2, 3] target = 4The possible co原创 2017-06-27 23:05:52 · 145 阅读 · 0 评论 -
Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and co原创 2017-07-17 15:06:15 · 167 阅读 · 0 评论 -
609. Find Duplicate File in System
Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in terms of their p原创 2017-06-22 10:12:35 · 263 阅读 · 0 评论 -
520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not.We define the usage of capitals in a word to be right when one of the following cases holds:All letters in this word原创 2017-06-22 09:48:28 · 124 阅读 · 0 评论 -
462. Minimum Moves to Equal Array Elements II
Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1原创 2017-06-22 09:43:33 · 170 阅读 · 0 评论 -
leetcode 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree.Example:3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.python i原创 2017-06-17 20:53:16 · 118 阅读 · 0 评论 -
230. Kth Smallest Element in a BST
My solution with a global variableclass Solution(object): def kthSmallest(self, root, k): """ :type root: TreeNode :type k: int :rtype: int """ self.原创 2017-06-17 21:14:18 · 117 阅读 · 0 评论 -
162. Find Peak Element
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks, in that case原创 2017-06-18 21:37:45 · 127 阅读 · 0 评论 -
173. Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next() and hasN原创 2017-06-18 22:56:56 · 142 阅读 · 0 评论 -
求解概率相关的题目的解法
Brief explanation for Reservoir Sampling111 WTIFS Reputation: 178PROBLEM:Choose k entries from n numbers. Make sure each number is selected with th原创 2017-06-28 20:01:16 · 283 阅读 · 0 评论 -
303. Range Sum Query - Immutable
class NumArray(object): def __init__(self, nums): """ :type nums: List[int] """ self.sums = [0] s = 0 for v in nums: s += v原创 2017-06-29 16:14:41 · 149 阅读 · 0 评论 -
300. Longest Increasing Subsequence
动态规划class Solution(object): def lengthOfLIS(self, nums): """ :type nums: List[int] :rtype: int """ if not nums: return 0 result = [num原创 2017-06-29 18:59:43 · 160 阅读 · 0 评论 -
565. Array Nesting
A zero-indexed array A consisting of N different integers is given. The array contains all integers in the range [0, N - 1].Sets S[K] for 0 <= K < N are defined as follows:S[K] = { A[K], A[A[K]], A[A[A原创 2017-06-22 09:35:30 · 198 阅读 · 0 评论 -
539. Minimum Time Difference
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list.Example 1:Input: ["23:59","00:00"]Output: 1N原创 2017-07-21 22:09:37 · 205 阅读 · 0 评论