![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 62
poiop221
这个作者很懒,什么都没留下…
展开
-
Best Time to Buy and Sell Stock II
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 on原创 2015-07-27 17:42:13 · 173 阅读 · 0 评论 -
Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]public class Solution {原创 2015-08-08 23:38:50 · 161 阅读 · 0 评论 -
Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element原创 2015-07-28 20:57:57 · 194 阅读 · 0 评论 -
Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element原创 2015-07-28 22:02:37 · 191 阅读 · 0 评论 -
Single Number
Given an array of integers, every element appears twice except for one. Find that single one.public class Solution { public int singleNumber(int[] nums) { int result = 0;原创 2015-07-28 17:48:29 · 203 阅读 · 0 评论 -
Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O原创 2015-07-28 22:29:02 · 234 阅读 · 0 评论 -
Binary Tree Level Order Traversal I & II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7},原创 2015-08-07 20:02:25 · 196 阅读 · 0 评论 -
combination
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]原创 2015-08-07 23:43:19 · 279 阅读 · 0 评论 -
Implement Stack using Queues
Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet原创 2015-08-08 22:11:54 · 187 阅读 · 0 评论 -
Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the tota原创 2015-08-08 21:14:48 · 217 阅读 · 0 评论 -
Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1]原创 2015-08-08 23:09:45 · 238 阅读 · 0 评论 -
Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value原创 2015-07-28 17:52:02 · 169 阅读 · 0 评论 -
Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3]./** * Definition for a原创 2015-07-28 17:45:10 · 189 阅读 · 0 评论 -
plus one
容易 加一查看运行结果 29%通过给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组。该数字按照大小进行排列,最大的数在列表的最前面。您在真实的面试中是否遇到过这个题? Yes样例给定 [1,2,3] 表示 123, 返回 [1,2,4].给定 [9,9,9] 表示 999, 返原创 2015-08-08 10:06:33 · 254 阅读 · 0 评论 -
combination sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited numb原创 2015-08-22 18:00:45 · 284 阅读 · 0 评论 -
Lowest Common Ancestor of a Binary Search Tree
Given 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 betw原创 2015-07-28 17:38:05 · 204 阅读 · 0 评论 -
Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursive solutio原创 2015-07-28 17:45:36 · 213 阅读 · 0 评论 -
Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1/** * Definition for a binary tree node. * public class TreeNode { *原创 2015-07-28 19:13:14 · 198 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.原创 2015-07-28 20:20:06 · 205 阅读 · 0 评论 -
Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value./** * Def原创 2015-07-28 17:46:20 · 190 阅读 · 0 评论 -
Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is原创 2015-08-23 21:34:23 · 237 阅读 · 0 评论 -
Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 000000原创 2015-07-28 17:40:09 · 187 阅读 · 0 评论 -
Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node./** * Definition for a binary tre原创 2015-07-28 17:52:27 · 239 阅读 · 0 评论 -
Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, return 5.原创 2015-08-24 21:59:03 · 190 阅读 · 0 评论 -
Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of原创 2015-08-24 22:44:28 · 235 阅读 · 0 评论 -
Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right.Integers in原创 2015-08-22 20:30:11 · 250 阅读 · 0 评论 -
subset II
中等 带重复元素的子集25%通过给定一个可能具有重复数字的列表,返回其所有可能的子集样例如果S = [1,2,2],一个可能的答案为:[ [2], [1], [1,2,2], [2,2], [1,2], []]注意子集中的每个元素都是非降序的两个子集间的顺序是无关紧要的解集原创 2015-05-12 10:04:07 · 302 阅读 · 0 评论 -
Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:What if t原创 2015-08-10 22:14:17 · 196 阅读 · 0 评论 -
Unique Binary Search Trees
Given 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 3 2 1 \原创 2015-07-29 21:09:23 · 262 阅读 · 0 评论 -
Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partially fille原创 2015-08-25 11:21:27 · 205 阅读 · 0 评论 -
power of two
Given an integer, write a function to determine if it is a power of two.public class Solution { public boolean isPowerOfTwo(int n) { return (n>0 && (n&(n-1)) == 0); }}如果一个数是2的次方,则原创 2015-08-12 10:11:13 · 248 阅读 · 0 评论 -
Implement Queue using Stacks
Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.原创 2015-07-30 22:19:03 · 189 阅读 · 0 评论 -
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()原创 2015-08-12 11:33:35 · 278 阅读 · 0 评论 -
path sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum原创 2015-08-12 10:56:17 · 234 阅读 · 0 评论 -
Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return [ ["aa","原创 2015-08-25 20:04:06 · 161 阅读 · 0 评论 -
Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.import java.util.Stack;public class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { if (inord原创 2015-08-26 21:03:36 · 221 阅读 · 0 评论 -
three sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c原创 2015-08-27 11:23:01 · 297 阅读 · 0 评论 -
roman to integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.public class Solution { public int romanToInt(String s) { if (s == null || s.原创 2015-07-30 23:13:50 · 224 阅读 · 0 评论 -
Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.public class Solution { public String intToRoman(int num) { if (num <= 0) { r原创 2015-07-31 00:05:53 · 192 阅读 · 0 评论 -
Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1]原创 2015-07-30 20:49:16 · 183 阅读 · 0 评论