
LeetCode
正努力学English的程序猿
正努力学English的程序猿
展开
-
【LeetCode】292.Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the原创 2015-12-01 11:53:26 · 341 阅读 · 0 评论 -
【LeetCode】228 Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges.For example, given [0,1,2,4,5,7], return [“0->2”,”4->5”,”7”].public class Solution { public List<String> summaryRan原创 2015-12-01 17:39:55 · 236 阅读 · 0 评论 -
【LeetCode】172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.Method 2 is original by myself. It is very close to Method 1, i need think mo原创 2015-12-19 18:24:08 · 367 阅读 · 0 评论 -
【LeetCode】88 Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional原创 2015-12-03 17:06:24 · 261 阅读 · 0 评论 -
【LeetCode】119 Pascal's Triangle II
Pascal’s TriangleGiven 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?Method 1原创 2015-12-03 13:58:21 · 363 阅读 · 0 评论 -
【LeetCode】118 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 { pub原创 2015-12-03 10:49:18 · 251 阅读 · 0 评论 -
【LeetCode】190 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 00111001011110原创 2015-12-03 09:45:23 · 266 阅读 · 0 评论 -
【LeetCode】27 Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn’t matter what you leave beyond the new length.Method 1:(原创 2015-12-03 09:49:03 · 265 阅读 · 0 评论 -
【LeetCode】67 Add Binary
Given two binary strings, return their sum (also a binary string).For example, a = “11” b = “1” Return “100”.Summary: Array[i] is faster than String.charAt(int i);Method 1 : (4ms)public class S原创 2015-12-04 17:59:29 · 416 阅读 · 0 评论 -
【LeetCode】219 Contains Duplicate II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.public clas原创 2015-12-04 16:28:27 · 312 阅读 · 0 评论 -
【LeetCode】104. Maximum Depth of Binary Tree
Question: 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.Solution:My solution:3ms/**原创 2016-03-24 11:02:06 · 296 阅读 · 0 评论 -
【LeetCode】226. Invert Binary Tree
Question:Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Solution:My solution (No recursion):1ms/** * Definition for a binary tre原创 2016-03-24 15:04:32 · 404 阅读 · 0 评论 -
【Leetcode】100. Same Tree
Question: 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 valu原创 2016-03-25 13:20:50 · 383 阅读 · 0 评论 -
【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.题意:给定一个非负整数用数组形式表示,最高位在数组下标最低原创 2015-12-02 09:27:03 · 258 阅读 · 0 评论 -
【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 cons原创 2015-12-01 18:13:37 · 300 阅读 · 0 评论 -
【LeetCode】189 Rotate Array
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Congradulation! My runtime beats 98.27% of java submissi原创 2015-12-02 13:21:14 · 256 阅读 · 0 评论 -
【LeetCode】258. Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, r原创 2015-12-01 12:00:52 · 347 阅读 · 0 评论 -
【LeetCode】283 Move Zeroes
Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your funct原创 2015-12-01 13:29:54 · 279 阅读 · 0 评论 -
【LeetCode】217 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 is原创 2015-12-01 13:33:01 · 308 阅读 · 0 评论 -
【LeetCode】242 Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.For example, s = “anagram”, t = “nagaram”, return true. s = “rat”, t = “car”, return false.Note: You may assume the s原创 2015-12-01 13:35:16 · 286 阅读 · 0 评论 -
【LeetCode】171 Excel Sheet Column Number
Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example:A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28 publ原创 2015-12-01 13:36:17 · 282 阅读 · 0 评论 -
【LeetCode】169 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 always原创 2015-12-01 13:37:57 · 323 阅读 · 0 评论 -
【LeetCode】191 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 0000000000000原创 2015-12-01 13:42:21 · 277 阅读 · 0 评论 -
【LeetCode】13 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) { char[] arrs = s.toCharArr原创 2015-12-01 13:50:46 · 257 阅读 · 0 评论 -
【LeetCode】70 Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?public class Solution { public in原创 2015-12-01 13:59:31 · 253 阅读 · 0 评论 -
【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 since it in原创 2015-12-01 14:37:53 · 255 阅读 · 0 评论 -
【LeetCode】231 Power of Two
Given an integer, write a function to determine if it is a power of two.思路: 求是否是2的幂,由2可以联想到2进制,只要数的2进制表示中只含有一个值为1的bit位,这个数即是2的幂。 法2用Integer.bitCount(int i)方法,该方法虽然只需一行即可解决问题,但是所需的时间却比自己写的法1更多原创 2015-12-01 17:11:48 · 339 阅读 · 0 评论 -
【LeetCode】202 Happy Number
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares原创 2015-12-01 16:12:47 · 287 阅读 · 0 评论 -
【LeetCode】206. Reverse Linked List
Question: Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both?My solution (recursion): 1ms/** * Definition for sing原创 2016-03-25 14:15:35 · 845 阅读 · 0 评论