- 博客(78)
- 收藏
- 关注
原创 [LeetCode]81. Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the
2016-03-23 09:26:23
267
原创 [LeetCode]119. Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?思路:之前的方法是利用动
2016-03-23 09:17:58
249
原创 [LeetCode]9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.思路:暂时没有什么好的方法,判断数字的位数,然后判断对称位置上的数字是否相同,想到新方法再来更吧public class Solution { public boolean isPalindrome(int x) { i
2016-03-23 09:12:59
289
原创 [LeetCode]338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example:For num = 5
2016-03-23 09:02:48
378
原创 [LeetCode]129. 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
2016-03-17 10:29:11
231
原创 [LeetCode]215. 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.
2016-03-17 10:27:29
226
原创 [LeetCode]334. Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.Formally the function should:Return true if there exists i, j, k such that arr[i] ar
2016-03-17 10:22:31
370
原创 [LeeiCode]77. Combinations
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],]
2016-03-17 10:19:53
203
原创 [LeetCode]172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.思路:5!= 120 则逢5 就会出现一个0;public class Solution { public int trailingZeroes(int n) { int s = 0; while (n>1) s += (
2016-03-17 10:18:06
236
原创 [LeetCode]289. Game of Life
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."Given a board with m
2016-03-15 09:47:03
346
原创 [LeetCode]118. Pascal's Triangle
Total Accepted: 77202 Total Submissions: 235367 Difficulty: EasyGiven numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1
2016-03-15 09:42:50
204
原创 [LeetCode]215. 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.
2016-03-15 09:38:34
177
原创 [LeetCode]117. Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant
2016-03-15 09:36:00
163
原创 [LeetCode]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
2016-03-15 09:33:50
184
原创 [LeetCode]66. Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.思路:判断最后一位是不是等于9,若等于9
2016-03-14 09:39:25
192
原创 [LeetCode]73. Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(m
2016-03-14 09:34:07
292
原创 [LeetCode]26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with
2016-03-14 09:30:11
210
原创 [LeetCode]27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.
2016-03-14 09:24:07
199
原创 [LeetCode]107. Binary Tree Level Order Traversal 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},
2016-03-14 09:20:20
271
原创 [LeetCode]74. Search a 2D Matrix
Total Accepted: 73109 Total Submissions: 218887 Difficulty: MediumWrite an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
2016-03-13 12:00:04
222
原创 [LeetCode]110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe
2016-03-13 11:51:40
165
原创 [LeetCode]199. Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tree, 1
2016-03-13 11:48:35
197
原创 [LeetCode]101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the f
2016-03-13 11:45:43
199
原创 [LeetCode]173. Binary Search Tree Iterator
mplement 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()
2016-03-12 10:18:48
166
原创 [LeetCode]154. Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Suppose a sorted array is rotated at some pivot u
2016-03-12 10:14:51
216
原创 [LeetCode]232. Implement Queue using Stackssil
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.empty(
2016-03-12 10:08:21
177
原创 [LeetCode]48. Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).思路:先求矩阵的转置,在按y轴对称public class Solution { public void rotate(int[][] matrix) {
2016-03-12 10:04:04
162
原创 [LeetCode]75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers
2016-03-12 09:59:45
305
原创 [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.采用动态规划的方法每一个格子拥有的值是它左边的格子拥有的值和它上边的格子拥
2016-03-11 10:42:44
192
原创 [LwwtCode]24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. Y
2016-03-11 10:36:57
169
原创 [LeetCode]145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].后序遍历啦,递归方法,so easy
2016-03-11 10:33:26
179
原创 [LeetCode]21. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.思路:先判断是不是有一个链表为空,如果有,则返回另一个链表如果两个链表均不为空,则新建一个节点储存新的链表
2016-03-11 10:23:55
205
原创 [LeetCode]89. Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of
2016-03-10 09:51:24
190
原创 [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
2016-03-10 09:44:54
159
原创 [LeetCode]153. Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicate exists in
2016-03-10 09:41:10
169
原创 [LeetCode]231. Power of Two
Given an integer, write a function to determine if it is a power of two.思路:判断一个数是不是2的乘方,根据数字在电脑中以二进制存储的特点,2的乘方只有1位是1,其他位均为0若n为2的乘方,则n-1与n的每一位都是不同的,因此n与n-1按位与的结果为0public class Solution { pub
2016-03-10 09:36:23
175
原创 [LeetCode]326. Power of Three
Given an integer, write a function to determine if it is a power of three.思路:乖乖计算出所有可能的结果。。。蓝后判断输入是不是和他们其中的一个相等。。。public class Solution { public boolean isPowerOfThree(int n) { return (n
2016-03-10 09:29:14
228
原创 [LeetCode]53. 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]
2016-03-09 12:04:16
178
原创 [LeeCode]116. 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.
2016-03-09 11:59:44
222
原创 [LeetCode]263. Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly sinc
2016-03-09 11:57:24
233
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人