刷题
liujunlovecs
这个作者很懒,什么都没留下…
展开
-
Longest Substring Without Repeating Characters
public class Solution { public int lengthOfLongestSubstring(String s) { if (s == null || s.length() == 0) { return 0; } Set set = new HashSet(); int lef原创 2015-06-16 10:15:35 · 207 阅读 · 0 评论 -
Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.public class Solution { public String mul原创 2015-06-24 09:59:22 · 202 阅读 · 0 评论 -
Symmetric Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2015-06-04 09:37:49 · 235 阅读 · 0 评论 -
Maximum Depth of Binary Tree
Recursive/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class S原创 2015-06-04 10:57:01 · 205 阅读 · 0 评论 -
Binary Tree Level Order Traversal II
Iterative/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class S原创 2015-06-04 10:49:55 · 165 阅读 · 0 评论 -
Minimum Depth of Binary Tree
Recursive/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class S原创 2015-06-04 13:27:06 · 198 阅读 · 0 评论 -
Path Sum
Recursive/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class S原创 2015-06-04 13:47:22 · 243 阅读 · 0 评论 -
Balanced Binary Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Sol原创 2015-06-04 09:14:54 · 252 阅读 · 0 评论 -
Reverse Integer
public class Solution { public int reverse(int x) { int result = 0; while (x != 0) { int newResult = result * 10 + x % 10; x = x / 10; if (newRe原创 2015-06-24 14:07:20 · 204 阅读 · 0 评论 -
Excel Sheet Column Number
public class Solution { public int titleToNumber(String s) { if (s == null || s.length() == 0) { return 0; } int result = 0; int i = s.length() - 1;原创 2015-06-24 13:57:13 · 196 阅读 · 0 评论 -
Count Primes
public class Solution { public int countPrimes(int n) { boolean[] isPrimes = new boolean[n]; for (int i = 2; i < n; i++) { isPrimes[i] = true; } for (in原创 2015-06-24 19:51:48 · 200 阅读 · 0 评论 -
Factorial Trailing Zeroes
public class Solution { public int trailingZeroes(int n) { if (n < 0) { return -1; } int count = 0; for (double i = 5; n / i >= 1; i *= 5) {//"double" t原创 2015-06-24 20:14:03 · 192 阅读 · 0 评论 -
Same Tree
Recursive/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class S原创 2015-06-04 10:00:17 · 250 阅读 · 0 评论 -
Binary Tree Level Order Traversal
Iterative/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class S原创 2015-06-04 13:56:02 · 236 阅读 · 0 评论 -
Excel Sheet Column Title
public class Solution { public String convertToTitle(int n) { if (n <= 0) { return null; } StringBuilder sb = new StringBuilder(); while (n > 0) {原创 2015-06-24 20:00:30 · 178 阅读 · 0 评论 -
Binary Tree Inorder Traversal
Iterative with Stack/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */pub原创 2015-06-04 14:26:00 · 210 阅读 · 0 评论 -
Binary Tree Preorder Traversal
Iterative with Stack/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */pub原创 2015-06-04 14:43:17 · 203 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node
Iterative/** * Definition for binary tree with next pointer. * public class TreeLinkNode { * int val; * TreeLinkNode left, right, next; * TreeLinkNode(int x) { val = x; } * } */p原创 2015-06-04 15:11:16 · 196 阅读 · 0 评论 -
Divide Two Integers
public class Solution { public int divide(int dividend, int divisor) { if (dividend == 0) { return 0; } if (divisor == 0) { return dividend > 0 ? In原创 2015-06-26 16:04:43 · 229 阅读 · 0 评论 -
Palindrome Number
public class Solution { public boolean isPalindrome(int x) { if (x < 0) { return false; } return x == reverse(x); } private int reverse (int x) {原创 2015-06-26 14:20:11 · 202 阅读 · 0 评论 -
Happy Number
public class Solution { public boolean isHappy(int n) { HashSet unhappy = new HashSet(); while (n != 1) { if (unhappy.contains(n)) { return false;原创 2015-06-26 14:43:33 · 188 阅读 · 0 评论 -
Faction to Recurring Decimal
public class Solution { public String fractionToDecimal(int numerator, int denominator) { if (numerator == 0) { return "0"; } if (denominator == 0) {原创 2015-06-26 15:47:22 · 277 阅读 · 0 评论 -
Climbing Stairs
public class Solution { public int climbStairs(int n) { if (n <= 1) { return 1; } int penultimate = 1, antepenultimate = 1; int last = 0; for (i原创 2015-06-07 14:15:52 · 226 阅读 · 0 评论 -
House Robber
public class Solution { public int rob(int[] nums) { int last = 0, penultimate = 0, antepenultimate = 0; for (int i = 0; i < nums.length; i++) { last = Math.max(antepen原创 2015-06-07 14:01:32 · 177 阅读 · 0 评论 -
Unique Path
public class Solution { public int uniquePaths(int m, int n) { if (m == 0 || n == 0) { return 0; } int[][] paths = new int[m][n]; for (int i = 0; i < m;原创 2015-06-08 15:19:36 · 213 阅读 · 0 评论 -
Unique Binary Search Trees
public class Solution { public int numTrees(int n) { int[] nums = new int[n + 1]; nums[0] = 1; nums[1] = 1; for (int i = 2; i <= n; i++) { for (int j =原创 2015-06-08 14:39:02 · 206 阅读 · 0 评论 -
House Robber II
public class Solution { public int rob(int[] nums) { if (nums.length == 0) { return 0; } if (nums.length == 1) { return nums[0]; } i原创 2015-06-08 14:23:11 · 220 阅读 · 0 评论 -
Maximum Subarray
public class Solution { public int maxSubArray(int[] nums) { int max = nums[0]; int[] sum = new int[nums.length]; sum[0] = nums[0]; for (int i = 1; i < nums.length;原创 2015-06-09 16:04:18 · 224 阅读 · 0 评论 -
Minimum Path Sum
public class Solution { public int minPathSum(int[][] grid) { int row = grid.length; int col = grid[0].length; int[][] sum = new int[row][col]; sum[0][0] = grid[0][原创 2015-06-09 15:53:17 · 288 阅读 · 0 评论 -
Best Time to Buy and Sell Stock
public class Solution { public int maxProfit(int[] prices) { if (prices == null || prices.length == 0) { return 0; } int low = Integer.MAX_VALUE; int pr原创 2015-06-09 12:11:32 · 200 阅读 · 0 评论 -
Maximum Product Subarray
public class Solution { public int maxProduct(int[] nums) { int[] max = new int[nums.length]; int[] min = new int[nums.length]; max[0] = nums[0]; min[0] = nums[0];原创 2015-06-09 14:51:04 · 210 阅读 · 0 评论 -
Minimum Size Subarray Sum
public class Solution { public int minSubArrayLen(int s, int[] nums) { if (nums == null || nums.length <1) { return 0; } int ptr1 = 0; int ptr2 = 0;原创 2015-07-07 20:45:28 · 272 阅读 · 0 评论 -
Number of Digit One
public class Solution { public int countDigitOne(int n) { int result = 0; for (long m = 1; m <= n; m *= 10) { long left = n / m; long right = n % m;原创 2015-07-09 16:07:01 · 234 阅读 · 0 评论 -
Min Stack
class MinStack { Node head; public void push(int x) { if (head == null) { head = new Node(x); } else { Node temp = new Node(x); temp.min = M原创 2015-07-08 12:33:31 · 324 阅读 · 0 评论 -
Number of 1 Bits
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { int count = 0; for (int i = 1; i < 33; i++) { if ((n & 1) != 0原创 2015-07-09 10:20:07 · 213 阅读 · 0 评论 -
Majority Element
public class Solution { public int majorityElement(int[] nums) { int result = nums[0]; int count = 1; for (int i = 1; i < nums.length; i++) { if (nums[i] == res原创 2015-07-01 20:31:24 · 195 阅读 · 0 评论 -
Rotate Array
public class Solution { public void rotate(int[] nums, int k) { if (nums == null || nums.length == 0) { return; } k = k % nums.length; reverse(nums, 0,原创 2015-07-01 21:32:34 · 170 阅读 · 0 评论 -
Search Insert Position
public class Solution { public int searchInsert(int[] nums, int target) { if (nums == null || nums.length < 1) { return -1; } int left = 0; int right =原创 2015-07-02 19:51:00 · 184 阅读 · 0 评论 -
Pascal's Triangle II
public class Solution { public List getRow(int rowIndex) { List result = new ArrayList(); if (rowIndex < 0) { return result; } result.add(1); fo原创 2015-07-01 20:07:06 · 172 阅读 · 0 评论 -
Contains Duplicate
public class Solution { public boolean containsDuplicate(int[] nums) { if (nums == null || nums.length == 0) { return false; } HashSet set = new HashSet();原创 2015-07-01 21:39:07 · 215 阅读 · 0 评论