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 on原创 2015-09-13 10:49:18 · 375 阅读 · 0 评论 -
Ugly Number II(求第N个丑数)
这个应该是微软的一道面试题,首先需要明白丑数的概念。1是一个特殊的丑数,其次只含有2,3,5质因数的数被定义为丑数。 关于解题的思路可以参考: http://www.geeksforgeeks.org/ugly-numbers/ 1到N的丑数为 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … ; 可以分成如下三组:(1) 1×2, 2×2, 3×2, 4×2, 5原创 2015-08-19 14:49:22 · 6913 阅读 · 2 评论 -
Same Tree
原题: 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 value.解题: 递归判断每个位置的原创 2015-08-26 21:31:31 · 330 阅读 · 0 评论 -
Group Anagrams
原题: 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原创 2015-08-25 14:23:01 · 3410 阅读 · 0 评论 -
Scramble String
原题: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = “great”:great/ \ gr eat原创 2015-08-24 20:40:04 · 354 阅读 · 0 评论 -
Best Time to Buy and Sell Stock II
原题: Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy on原创 2015-08-22 16:15:13 · 345 阅读 · 0 评论 -
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.解题: 寻找连续的N个数中缺失的那个数,那么直接用原来N个数的和减去原创 2015-08-24 09:46:18 · 568 阅读 · 0 评论 -
Best Time to Buy and Sell Stock III
原题: Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note: You may not原创 2015-08-24 23:28:47 · 392 阅读 · 0 评论 -
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原创 2015-08-24 18:40:05 · 313 阅读 · 0 评论 -
House Robber II
原题: Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time原创 2015-08-23 18:45:38 · 295 阅读 · 0 评论 -
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 h原创 2015-08-23 14:03:04 · 302 阅读 · 0 评论 -
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 possible input ca原创 2015-08-22 14:30:23 · 352 阅读 · 0 评论 -
Invert Binary Tree
原题: Invert a binary tree.4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was inspired by this original tweet by Max How原创 2015-08-22 14:20:02 · 377 阅读 · 0 评论 -
Majority Element II
原题: Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.解题: 这个其实跟编程之美上面的“寻找发帖水王”的题如出一辙,也就是寻找一个数组中超过一半的元素原创 2015-08-22 13:28:31 · 371 阅读 · 0 评论 -
Reorder List
原题如下:Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reo原创 2015-08-18 23:54:04 · 594 阅读 · 0 评论 -
Interleaving String
原题如下: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example, Given: s1 = “aabcc”, s2 = “dbbca”,When s3 = “aadbbcbcac”, return true. When s3 = “aadbbbaccc”, retur原创 2015-08-19 11:04:20 · 746 阅读 · 0 评论 -
Unique Paths
原题如下(链接:https://leetcode.com/problems/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 an原创 2015-08-19 19:00:19 · 817 阅读 · 0 评论 -
Reverse Nodes in k-Group
原题: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is原创 2015-08-22 16:02:58 · 338 阅读 · 0 评论 -
Reverse Linked List II
原题: Reverse a linked list from position m to n. Do it in-place and in one-pass.For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note: Given m, n satisfy the followi原创 2015-08-21 22:28:21 · 893 阅读 · 0 评论 -
H-Index
原题如下: Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher’s h-index.According to the definition of h-index on Wikipedia: “原创 2015-09-07 10:30:01 · 526 阅读 · 0 评论 -
Spiral Matrix
原题: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example, Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Y原创 2015-08-20 13:52:23 · 878 阅读 · 0 评论 -
Search a 2D Matrix
原题如下: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right. The first integer of each原创 2015-08-20 10:43:46 · 589 阅读 · 0 评论 -
Sliding Window Maximum
原题: Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding wind原创 2015-08-22 00:25:59 · 908 阅读 · 0 评论 -
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-08-22 00:06:13 · 817 阅读 · 0 评论 -
Container With Most Water
原题: Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find tw原创 2015-08-21 10:53:23 · 638 阅读 · 0 评论 -
Gas Station
原题: There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to it原创 2015-08-20 16:51:06 · 355 阅读 · 0 评论 -
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.Note: You can only move either down or right at原创 2015-08-20 15:16:53 · 679 阅读 · 0 评论 -
Rotate List
原题: Given a list, rotate the list to the right by k places, where k is non-negative.For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.解题: 跟旋转字符串的题类似,k为非负数,k为0时原串返回,k>0时对链原创 2015-08-20 17:34:07 · 265 阅读 · 0 评论 -
Binary Tree Preorder Traversal
原题: Given a binary tree, return the preorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3].Note: Recursive solution is tri原创 2015-08-20 14:12:26 · 747 阅读 · 0 评论 -
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 in原创 2015-08-20 10:57:06 · 1032 阅读 · 0 评论 -
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 此题的意思就是根据根除的整数转换成excel表格里面我们常见的英文字母表示法,把原创 2015-04-03 11:16:22 · 591 阅读 · 0 评论 -
Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be very larg原创 2015-04-03 11:10:22 · 802 阅读 · 0 评论 -
First Missing Positive
Given an unsorted integer array, find the first missing positive integer.For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant space.原创 2015-04-03 11:26:20 · 716 阅读 · 0 评论 -
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 solutions as you can, th原创 2015-03-20 11:00:27 · 3101 阅读 · 0 评论 -
Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You should pack your words in a greedy approach; that is, p原创 2015-03-19 14:24:16 · 3647 阅读 · 0 评论 -
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 000000000000原创 2015-03-19 14:37:29 · 5447 阅读 · 0 评论 -
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, where i原创 2015-03-19 14:47:50 · 4873 阅读 · 0 评论 -
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-03-19 14:42:55 · 4652 阅读 · 0 评论 -
Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) – Push element x onto stack.pop() – Removes the element on top of the stack.top() – Get the t原创 2015-03-19 14:51:49 · 4627 阅读 · 0 评论 -
Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x. 实现sqrt(int x)函数。class Solution: # @param x, an integer # @return an integer def sqrt(self, x): if x == 0 :原创 2015-03-19 14:22:11 · 6416 阅读 · 0 评论