LeetCode
文章平均质量分 53
chenofzju
二货一枚快乐多
展开
-
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 case原创 2015-03-25 23:15:49 · 267 阅读 · 0 评论 -
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.Solution:class Solution {public: string longestCommonPrefix(vector &strs) { if(strs.size() ==原创 2015-03-26 00:09:23 · 242 阅读 · 0 评论 -
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 [1, 2, 3,原创 2015-03-26 23:39:52 · 245 阅读 · 0 评论 -
Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without an原创 2015-03-28 15:18:03 · 227 阅读 · 0 评论 -
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?Solution:class S原创 2015-03-10 21:10:35 · 276 阅读 · 0 评论 -
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]]Solution:class Solution原创 2015-03-10 17:32:13 · 260 阅读 · 0 评论 -
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.Solution:class Sol原创 2015-03-11 18:55:28 · 263 阅读 · 0 评论 -
Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.Solution:class Solution {private: static const int MAX_INT = 0x7fffffff;原创 2015-03-21 11:17:01 · 233 阅读 · 0 评论 -
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 any point in time. The robot is trying to reach the bo原创 2015-03-11 20:44:48 · 273 阅读 · 0 评论 -
Pow(x, n)
Implement pow(x, n).Solution:class Solution {public: double pow(double x, int n) { if(!n) return 1; double num; double half = pow(x, n/2); if(n % 2 == 0) n原创 2015-03-11 00:15:25 · 3714 阅读 · 0 评论 -
Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321Have you thought about this?Here are some good questions to ask before coding. Bonus points for you原创 2015-03-11 13:06:05 · 2183 阅读 · 0 评论 -
Unique Paths II
Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the grid.原创 2015-03-11 22:04:52 · 247 阅读 · 0 评论 -
Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Examp原创 2015-03-22 01:08:07 · 243 阅读 · 0 评论 -
LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:get and set.get(key) - Get the value (will always be positive) of the key if t原创 2015-03-29 15:25:58 · 365 阅读 · 0 评论 -
Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.For example,X X X XX O O XX X O原创 2015-03-29 21:36:21 · 225 阅读 · 0 评论 -
Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.原创 2015-03-22 23:56:51 · 246 阅读 · 0 评论 -
Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.Solution:class Solution {public: int sqrt(int x) { int left = 1, right = x; while(left <= right)原创 2015-03-22 23:36:06 · 323 阅读 · 0 评论 -
Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string,原创 2015-03-12 19:28:14 · 244 阅读 · 0 评论 -
Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""231""312""原创 2015-03-13 12:40:21 · 257 阅读 · 0 评论 -
Merge Intervals
Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].Solution:/** * Definition for an interval. * s原创 2015-03-13 17:01:52 · 269 阅读 · 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-03-22 17:36:25 · 258 阅读 · 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-03-22 21:49:02 · 264 阅读 · 0 评论 -
Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.Solution:class Solution {public: int romanToInt(string s) { int num = 0原创 2015-03-14 15:40:37 · 233 阅读 · 0 评论 -
Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.Solution:class Solution {public: string intToRoman(int num) { string ro原创 2015-03-14 16:14:00 · 248 阅读 · 0 评论 -
Valid Number
Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambiguous.原创 2015-03-13 21:27:32 · 347 阅读 · 0 评论 -
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?Solution:class Solution {原创 2015-03-13 21:39:59 · 191 阅读 · 0 评论 -
Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its原创 2015-03-25 00:55:10 · 241 阅读 · 0 评论 -
Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the arr原创 2015-03-25 00:58:18 · 255 阅读 · 0 评论 -
Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of itsneighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use # as a separator for each原创 2015-04-06 00:05:45 · 299 阅读 · 0 评论 -
Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence fromstart to end, such that:Only one letter can be changed at a timeEach intermediate word原创 2015-04-06 19:39:11 · 255 阅读 · 0 评论 -
Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) fromstart to end, such that:Only one letter can be changed at a timeEach intermediate word must ex原创 2015-07-08 20:54:10 · 289 阅读 · 0 评论 -
Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2原创 2015-07-08 22:06:39 · 248 阅读 · 0 评论 -
Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Update (2014-11-02):The signature of the function had been updated to原创 2015-07-10 00:28:56 · 195 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.Solution:/** * Definition for singly-linked list. * struct ListNode { * int原创 2015-07-25 23:57:39 · 236 阅读 · 0 评论 -
Simplify Path
Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"click to show corner cases.Corner Cases:Did you co原创 2015-07-12 02:04:56 · 284 阅读 · 0 评论 -
Permutations
Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1],[3,1,2], and [3,2,1].Solution:cla原创 2015-07-25 01:43:54 · 282 阅读 · 0 评论 -
Anagrams
Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.Solution:class Solution {public: vector anagrams(vector& strs) {原创 2015-07-11 01:28:32 · 198 阅读 · 0 评论 -
Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.Solution:/** * Definition for a binary tree node. * struct TreeNode { * int val; *原创 2015-07-25 20:25:15 · 229 阅读 · 0 评论 -
Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.The原创 2015-07-26 19:08:20 · 205 阅读 · 0 评论 -
Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].原创 2015-07-25 13:45:50 · 189 阅读 · 0 评论