leetcode
zekizz
这个作者很懒,什么都没留下…
展开
-
leetcode 91. Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of w原创 2017-06-11 23:41:16 · 403 阅读 · 0 评论 -
leetcode 198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses原创 2017-06-09 20:33:18 · 256 阅读 · 0 评论 -
leetcode 309. Best Time to Buy and Sell Stock with Cooldown
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and原创 2017-06-09 18:44:00 · 299 阅读 · 0 评论 -
leetcode 62. Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the botto原创 2017-06-09 16:59:17 · 282 阅读 · 0 评论 -
leetcode 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 com原创 2017-06-09 16:48:47 · 670 阅读 · 0 评论 -
leetcode 486. Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a原创 2017-06-08 21:23:23 · 325 阅读 · 0 评论 -
将二叉搜索树转化为双向链表
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。创建的双向链表的顺序就是二叉搜索树的中序遍历结果。 这里有递归和非递归解法。一般的非递归解法需要开辟额外的空间,本次借助Morris算法,给出一种不需要额外空间开销的非递归解法。 首先是递归 TreeNode lastLeft = null; public Tree原创 2017-06-20 20:21:37 · 606 阅读 · 0 评论 -
leetcode 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-08 20:16:02 · 289 阅读 · 0 评论 -
leetcode 357. Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.Example:Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excludin原创 2017-06-08 19:41:39 · 300 阅读 · 0 评论 -
复制含有随机节点的链表
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)由于是深copy,随机节点指向的对象也应该是新的节点。 给出两种解法 第一种,采用HashMap记录新旧节点 代码如下: public RandomListNode Clone原创 2017-06-20 18:25:43 · 520 阅读 · 0 评论 -
剑指offer 二叉树的后续遍历序列
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。这题不难,今天想到一个比较好的递归的解法。public class Solution { public boolean VerifySquenceOfBST(int [] sequence) { if(sequence==null || s原创 2017-06-20 17:06:48 · 368 阅读 · 0 评论 -
leetcode 413. Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.For example, these are arithmetic sequence:1,原创 2017-06-08 17:30:35 · 257 阅读 · 0 评论 -
剑指offer 数值的整数次方
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。写出指数的二进制表达,例如13表达为二进制1101。 举例:10^1101 = 10^0001*10^0100*10^1000。 通过&1和>>1来逐位读取1101,为1时将该位代表的乘数累乘到最终结果。代码public class Solution { public doubl原创 2017-06-20 10:36:47 · 369 阅读 · 0 评论 -
leetcode 64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at any原创 2017-06-09 21:29:09 · 263 阅读 · 0 评论 -
leetcode 300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore t原创 2017-06-09 23:48:07 · 262 阅读 · 0 评论 -
leetcode 523. Continuous Subarray Sum
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n原创 2017-06-11 23:16:52 · 416 阅读 · 0 评论 -
leetcode 322. Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money c原创 2017-06-11 20:27:07 · 321 阅读 · 0 评论 -
leetcode 221. Maximal Square & 85. Maximal Rectangle
221. Maximal SquareGiven a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1原创 2017-06-11 17:51:47 · 386 阅读 · 0 评论 -
leetcode 139. Word Break
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assum原创 2017-06-11 16:14:43 · 438 阅读 · 0 评论 -
leetcode 63. Unique Paths II
Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the grid.For ex原创 2017-06-11 00:19:02 · 340 阅读 · 0 评论 -
leetcode 264. Ugly Number II
Total Accepted: 56978Total Submissions: 176871Difficulty: MediumContributor: LeetCodeWrite a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include原创 2017-06-10 23:57:24 · 370 阅读 · 0 评论 -
leetcode 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5,7],原创 2017-06-10 23:25:43 · 311 阅读 · 0 评论 -
leetcode 213. House Robber II
Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all原创 2017-06-10 23:04:59 · 316 阅读 · 0 评论 -
leetcode 376. Wiggle Subsequence
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either posi原创 2017-06-10 21:17:08 · 334 阅读 · 0 评论 -
leetcode 374&375. Guess Number Higher or Lower
We are playing the Guess Game. The game is as follows:I pick a number from 1 to n. You have to guess which number I picked.Every time you guess wrong, I'll tell you whether the number is higher or lowe原创 2017-06-10 19:40:21 · 336 阅读 · 0 评论 -
leetcode 410. Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m suba原创 2017-06-10 16:12:38 · 687 阅读 · 0 评论 -
279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, retu原创 2017-06-10 15:07:00 · 255 阅读 · 0 评论 -
leetcode 338. Counting Bits
Total Accepted: 76419Total Submissions: 125855Difficulty: MediumContributor: LeetCodeGiven a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1'原创 2017-06-07 23:19:26 · 278 阅读 · 0 评论 -
leetcode 416. Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.Note:Each of the array element w原创 2017-06-07 22:44:44 · 342 阅读 · 0 评论 -
leetcode 392. Is Subsequence
Given a string s and a string t, check if s is subsequence of t.You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, an原创 2017-06-07 15:12:30 · 298 阅读 · 0 评论 -
leetcode 235&236
235Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between原创 2017-05-19 16:11:56 · 2014 阅读 · 0 评论 -
leetcode 572. Subtree of Another Tree
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node'原创 2017-05-19 14:44:02 · 460 阅读 · 0 评论 -
leetcode 563. Binary Tree Tilt
Given a binary tree, return the tilt of the whole tree.The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree nod原创 2017-05-19 11:11:41 · 315 阅读 · 0 评论 -
leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \ 2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]分析: 这题递归的解法要点是要记住到达父节点的路径之原创 2017-05-19 10:50:56 · 249 阅读 · 0 评论 -
leetcode 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree.如果采用递归的解法,就需要判断前后两次迭代的关系。 如果树只有一个节点,需要判断其与父节点的关系,如果该节点是左孩子,就直接返回其值,如果是右孩子就返回0。如果还有孩子,就往下递归。 注意到这里有个与父节点关系,按照一般的递归是无法保持这个信息,同时为了递归的简便,不太方便在每次递归里还原创 2017-05-19 10:12:26 · 667 阅读 · 0 评论 -
leetcode 105&106
105. Construct Binary Tree from Preorder and Inorder Traversal106. Construct Binary Tree from Inorder and Postorder TraversalNote:You may assume that duplicates do not exist in the tree.将手动解法规则化,主要是原创 2017-05-26 19:47:26 · 872 阅读 · 0 评论 -
leetcode 99. Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a consta原创 2017-05-26 15:19:52 · 337 阅读 · 0 评论 -
leetcode 98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.The right原创 2017-05-26 10:57:51 · 281 阅读 · 0 评论 -
leetcode 95&96. Unique Binary Search Trees
96. Unique Binary Search TreesGiven n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3原创 2017-05-25 23:13:36 · 457 阅读 · 0 评论 -
leetcode 508. Most Frequent Subtree Sum
Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (includin原创 2017-05-25 19:43:24 · 283 阅读 · 0 评论