- 博客(28)
- 收藏
- 关注
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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
原创 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 2190
原创 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 3753
原创 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
原创 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
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人