![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode_JAVA
文章平均质量分 53
WayneShan
这个作者很懒,什么都没留下…
展开
-
【LeetCode】【JAVA】【047】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]]思路:用两层循环逐层求值原创 2015-02-22 09:18:27 · 137 阅读 · 0 评论 -
【LeetCode】【JAVA】【057】Longest Consecutive Sequence
题目:Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [原创 2015-02-26 01:50:06 · 140 阅读 · 0 评论 -
【LeetCode】【JAVA】【049】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原创 2015-02-24 04:26:08 · 112 阅读 · 0 评论 -
【LeetCode】【JAVA】【051】Merge Sorted Array
题目:Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional element原创 2015-02-24 07:20:11 · 204 阅读 · 0 评论 -
【LeetCode】【JAVA】【058】Palindrome Number
题目:Determine whether an integer is a palindrome. Do this without extra space.思路:做太多Array List,都忘了怎么处理 Int基本上就是 取余数,除10,还有求 Int 数的位数Code: public boolean isPalindrome(int x) {原创 2015-02-26 02:20:54 · 123 阅读 · 0 评论 -
【LeetCode】【JAVA】【067】Submission Details
题目:Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.思路:末尾的0 都由 2*5 得,2的数目比5的多,所以计算5的数目就可以。具体看:http://www.cnbl原创 2015-03-26 08:57:28 · 138 阅读 · 0 评论 -
【LeetCode】【JAVA】【066】Search for a Range
题目:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found原创 2015-03-26 08:33:05 · 109 阅读 · 0 评论 -
【LeetCode】【JAVA】【068】Intersection of Two Linked Lists
题目:Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘原创 2015-03-26 09:34:53 · 126 阅读 · 0 评论 -
【LeetCode】【JAVA】【069】Combination Sum
题目:Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain原创 2015-03-26 13:19:13 · 123 阅读 · 0 评论 -
【LeetCode】【JAVA】【070】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. 1原创 2015-03-26 14:23:05 · 113 阅读 · 0 评论 -
【LeetCode】【JAVA】【048】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,原创 2015-02-24 00:36:41 · 190 阅读 · 0 评论 -
【LeetCode】【JAVA】【056】Minimum Depth of Binary Tree
题目:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.思路:DFS,Recursion原创 2015-02-25 05:06:42 · 132 阅读 · 0 评论 -
【LeetCode】【JAVA】【035】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 consta原创 2015-02-18 08:48:43 · 99 阅读 · 0 评论 -
【LeetCode】【JAVA】【037】Spiral Matrix II
题目:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9,原创 2015-02-20 06:03:38 · 441 阅读 · 0 评论 -
【LeetCode】【JAVA】【042】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.Follow up:Did you use extra space?A straight forward solution using O(mn) space is probably a原创 2015-02-21 02:22:33 · 132 阅读 · 0 评论 -
【LeetCode】【JAVA】【043】Linked List Cycle II
题目:Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up:Can you solve it without using extra space?思路1:具体看文章:http://blog.sina.原创 2015-02-21 13:30:00 · 124 阅读 · 0 评论 -
【LeetCode】【JAVA】【054】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?原创 2015-02-25 04:28:47 · 131 阅读 · 0 评论 -
【LeetCode】【JAVA】【053】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原创 2015-02-24 09:02:20 · 150 阅读 · 0 评论 -
【LeetCode】【JAVA】【050】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 total原创 2015-02-24 06:57:27 · 114 阅读 · 0 评论 -
【LeetCode】【JAVA】【055】Binary Tree Level Order Traversal
题目:Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20原创 2015-02-25 04:48:35 · 133 阅读 · 0 评论 -
【LeetCode】【JAVA】【052】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 a原创 2015-02-24 07:48:35 · 143 阅读 · 0 评论 -
【LeetCode】【JAVA】【071】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原创 2015-03-26 23:10:05 · 118 阅读 · 0 评论 -
【LeetCode】【JAVA】【072】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.Determ原创 2015-03-26 23:55:19 · 143 阅读 · 0 评论 -
【LeetCode】【JAVA】【077】Path Sum II
题目:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5原创 2015-04-03 09:42:14 · 150 阅读 · 0 评论 -
【LeetCode】【JAVA】【080】Two Sum
题目:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target,原创 2015-04-03 11:37:35 · 136 阅读 · 0 评论 -
【LeetCode】【JAVA】【079】Pow(x, n)
题目:Implement pow(x, n).思路:使用二分法:pow(x,n) = pow(x,n/2)*pow(x,n-n/2)Code: public double pow(double x, int n) { if(n<0){ if( n == Integer.MIN_VALUE)原创 2015-04-03 10:56:42 · 190 阅读 · 0 评论 -
【LeetCode】【JAVA】【076】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 ex原创 2015-04-03 08:43:46 · 155 阅读 · 0 评论 -
【LeetCode】【JAVA】【081】Word Break
题目:http://oj.leetcode.com/problems/word-break/思路:DP,res[i] 的 boolean 数组记录第 ith 位可否被 break。res[ j ] && wordDict.contains( s.substring( j , i+1 )) 判断第 ith 位可否被 break。遍历检测,知道 string 的最后一位。参考:ht原创 2015-04-29 03:28:39 · 153 阅读 · 0 评论 -
【LeetCode】【JAVA】【083】Happy Number
题目: https://leetcode.com/problems/happy-number/思路:用 HashSet 检查重复。另外有地方在讨论 Code: public boolean isHappy(int n) { if(n==1) return true; HashSet hs = new HashSet();原创 2015-04-29 22:06:02 · 167 阅读 · 0 评论 -
【LeetCode】【JAVA】【084】Remove Linked List Elements
题目:https://leetcode.com/problems/number-of-1-bits/思路:NothingCode: public ListNode removeElements(ListNode head, int val) { while(head != null && head.val==val){原创 2015-04-29 22:31:13 · 169 阅读 · 0 评论 -
【LeetCode】【JAVA】【082】Number of 1 Bits
题目:https://leetcode.com/problems/number-of-1-bits/思路:n = n&(n-1) 可去除最右边的1,直到 n=0Code: public int hammingWeight(int n) { if(n==0) return 0; int res =0; whil原创 2015-04-29 04:44:17 · 128 阅读 · 0 评论 -
【LeetCode】【JAVA】【085】Word Break II
题目:https://leetcode.com/problems/word-break-ii/思路:直接用 Recursion超时。取 Word Break 的思路,先 DP 获得 jth 位置可达到的 index,再通过 Recursion 获得所有结果。Code:public class Solution { ArrayList resAL;原创 2015-04-29 23:29:34 · 159 阅读 · 0 评论 -
【LeetCode】【JAVA】【078】Valid Parentheses
题目:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all val原创 2015-04-03 10:19:31 · 118 阅读 · 0 评论 -
【LeetCode】【JAVA】【065】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 num原创 2015-03-24 23:53:04 · 195 阅读 · 0 评论 -
【LeetCode】【JAVA】【073】Reverse Bits
题目:Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 001110原创 2015-03-27 00:14:07 · 243 阅读 · 0 评论 -
【LeetCode】【JAVA】【059】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-03-23 22:39:35 · 160 阅读 · 0 评论 -
【LeetCode】【JAVA】【061】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-03-24 11:31:14 · 184 阅读 · 0 评论 -
【LeetCode】【JAVA】【062】Subsets
题目:Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,原创 2015-03-24 12:11:29 · 154 阅读 · 0 评论 -
【LeetCode】【JAVA】【074】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原创 2015-04-02 10:28:33 · 323 阅读 · 0 评论 -
【LeetCode】【JAVA】【075】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],原创 2015-04-03 02:03:53 · 122 阅读 · 0 评论