
LeetCode
文章平均质量分 62
fullstack_lth
这个作者很懒,什么都没留下…
展开
-
LeetCode: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原创 2015-10-28 12:57:49 · 278 阅读 · 0 评论 -
LeetCode: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 a原创 2015-10-27 14:56:36 · 309 阅读 · 0 评论 -
LeetCode: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原创 2015-10-25 23:58:57 · 327 阅读 · 0 评论 -
LeetCode:Sqrt(x)
问题描述:Implement int sqrt(int x).Compute and return the square root of x.1、开始我想到的是二分法,但是貌似二分法还是超时了。class Solution {public: int mySqrt(int x) { int low = 1; i原创 2015-10-21 17:49:16 · 317 阅读 · 0 评论 -
LeetCode:String to Integer (atoi)
问题描述:Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possib原创 2015-10-28 16:43:37 · 308 阅读 · 0 评论 -
LeetCode:First Bad Version
问题描述:You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based原创 2015-10-27 23:40:43 · 329 阅读 · 0 评论 -
LeetCode:Single Number
问题描述:Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it withou原创 2015-10-27 16:16:00 · 279 阅读 · 0 评论 -
LeetCode:Remove Linked List Elements
问题描述:Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5简单说就是去掉链表中指定值的算原创 2015-10-24 22:28:56 · 386 阅读 · 0 评论 -
LeetCode:Missing Number
问题描述:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.Note:Your algo原创 2015-10-28 22:31:11 · 336 阅读 · 0 评论 -
LeetCode: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原创 2015-10-26 23:16:55 · 403 阅读 · 0 评论 -
LeetCode: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原创 2015-10-29 00:32:35 · 291 阅读 · 0 评论 -
LeetCode:Power of Two
问题描述:Given an integer, write a function to determine if it is a power of two.Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.思路:如果是po原创 2015-10-27 13:33:29 · 285 阅读 · 0 评论 -
LeetCode: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 majori原创 2015-10-26 00:22:17 · 358 阅读 · 0 评论 -
LeetCode: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?Subscribe to原创 2015-10-29 09:51:18 · 277 阅读 · 0 评论 -
LeetCode: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 representa原创 2015-10-27 14:36:38 · 340 阅读 · 0 评论 -
LeetCode: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原创 2015-10-26 16:08:11 · 394 阅读 · 0 评论 -
LeetCode:Reverse Integer
问题描述:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to a原创 2015-10-26 14:43:02 · 382 阅读 · 0 评论 -
LeetCode: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]. Note:Try to come up as many soluti原创 2015-10-27 00:25:22 · 378 阅读 · 0 评论 -
LeetCode: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 no原创 2015-10-26 15:09:25 · 351 阅读 · 0 评论 -
LeetCode:Invert Binary Tree
问题描述:Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1思路:基础题目,提供两个版本:递归和非递归版本JAVA代码:递归版本:public class Soluti原创 2015-11-16 13:52:25 · 357 阅读 · 0 评论 -
LeetCode:Word Pattern
问题描述:Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty wor原创 2015-10-29 22:36:59 · 342 阅读 · 0 评论 -
LeetCode: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:原创 2015-11-16 12:37:48 · 302 阅读 · 0 评论 -
LeetCode:Compare Version Numbers
问题描述:Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0.You may assume that the version strings are non-e原创 2015-11-16 14:57:21 · 305 阅读 · 0 评论 -
LeetCode: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 def原创 2015-11-16 15:40:24 · 299 阅读 · 0 评论 -
LeetCode:Binary Tree Paths
问题描述:Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]原创 2015-11-16 12:29:31 · 294 阅读 · 0 评论 -
LeetCode:Excel Sheet Column Title
问题描述:Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB原创 2015-11-20 16:14:28 · 316 阅读 · 0 评论 -
LeetCode:Range Sum Query - Immutable
问题描述:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -原创 2015-11-16 10:11:13 · 337 阅读 · 0 评论 -
LeetCode:Bulls and Cows
问题描述:You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provi原创 2015-11-16 11:53:32 · 387 阅读 · 0 评论 -
LeetCode: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"].思路:两个指针 s,e . 如果nums[e+1] = nums[e]+1原创 2015-11-16 13:05:47 · 401 阅读 · 0 评论 -
LeetCode: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 ele原创 2015-11-17 09:17:52 · 353 阅读 · 0 评论 -
LeetCode: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() -- Re原创 2015-11-17 10:47:40 · 344 阅读 · 0 评论 -
LeetCode:Count Primes
问题描述:Count the number of prime numbers less than a non-negative number, n.返回n之前所有素数的个数。思路:朴素算法代码1显然不满足,初始想法就是朴素算法,没有通过,代码2为埃拉托斯特尼筛法,可以AC。代码1:初始想法:class Solution {public: int countPr原创 2015-12-09 10:25:10 · 314 阅读 · 0 评论 -
LeetCode:Palindrome Number
问题描述:Determine whether an integer is a palindrome. Do this without extra space.思路:1、求出位数;2、取出数的第一位和最后一位比较,若相同,将第一位和最后一位同时去掉,然后将base维度降低100,再比较新数的头尾,如此循环下去;如果不等,直接返回false。代码:代码1class Solu原创 2015-12-09 12:04:17 · 337 阅读 · 0 评论 -
LeetCode:Isomorphic Strings
问题描述:Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replace原创 2015-12-09 13:52:33 · 332 阅读 · 0 评论 -
LeetCode: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原创 2015-12-10 12:08:54 · 272 阅读 · 0 评论 -
LeetCode:Factorial Trailing Zeroes
问题描述:Given an integer n, return the number of trailing zeroes in n!.计算n!中0的个数。思路:对n!做质因数分解n!=2x*3y*5z*...显然0的个数等于min(x,z),并且min(x,z)==z证明:对于阶乘而言,也就是1*2*3*...*n[n/k]代表1~n中原创 2015-11-17 17:28:38 · 284 阅读 · 0 评论 -
LeetCode:Roman to Integer
问题描述:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.思路:纯模拟:基本字符IVXLCDM相应的原创 2015-11-17 15:57:10 · 374 阅读 · 0 评论 -
LeetCode: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 l原创 2015-11-17 16:43:22 · 293 阅读 · 0 评论 -
LeetCode: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 mo原创 2015-11-17 17:18:52 · 365 阅读 · 0 评论 -
LeetCode: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 adj原创 2015-11-18 21:47:19 · 314 阅读 · 0 评论