10 2015 档案
摘要:Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.For example:Given n = 13,Return 6...
阅读全文
摘要:A peak element is an element that is greater than its neighbors.Given an input array wherenum[i] ≠ num[i+1], find a peak element and return its index....
阅读全文
摘要:Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Write a function to...
阅读全文
摘要:Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).You are given a target value t...
阅读全文
摘要:Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).Find the minimum element.The a...
阅读全文
摘要:Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).Find the minimum element.You m...
阅读全文
摘要:Given an array ofnintegers wheren> 1,nums, return an arrayoutputsuch thatoutput[i]is equal to the product of all the elements ofnumsexceptnums[i].Solv...
阅读全文
摘要:Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.You may assume each number in the sequ...
阅读全文
摘要:Implementint sqrt(int x).Compute and return the square root ofx.解题思路:对于一个非负数n,它的平方根不会大于(n/2+1)。在[0, n/2+1]这个范围内可以进行二分搜索(binary search),求出n的平方根。注:在中间过程...
阅读全文
摘要:Implement pow(x,n).解题思路:pow(x,n)就是求x的n次方。x的N次方可以看做:x^n = x^(n/2)*x^(n/2)*x^(n%2)。所以利用递归求解,当n==1的时候,x^n=x。当然n是可以小于0的,2^(-3) = 1/(2^3)。按照上面那个规律就可以解决了。Ja...
阅读全文
摘要:Given an array containingndistinct numbers taken from0, 1, 2, ..., n, find the one that is missing from the array.For example,Givennums=[0, 1, 3]retur...
阅读全文
摘要:Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.For exampl...
阅读全文
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array[2,3,-2,4],the...
阅读全文
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,...
阅读全文
摘要:Write a program to find then-th ugly number.Ugly numbers are positive numbers whose prime factors only include2, 3, 5. For example,1, 2, 3, 4, 5, 6, 8...
阅读全文
摘要: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 i...
阅读全文
摘要:A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The robot can only move either down or right at any point ...
阅读全文
摘要:Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:Yo...
阅读全文
摘要:Note:This is an extension ofHouse Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that h...
阅读全文
摘要:There are a row ofnhouses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain ...
阅读全文
摘要:Given an arraySofnintegers, are there elementsa,b,c, anddinSsuch thata+b+c+d= target? Find all unique quadruplets in the array which gives the sum of ...
阅读全文
摘要:Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:Elemen...
阅读全文
摘要:Given an array of integers that is alreadysorted in ascending order, find two numbers such that they add up to a specific target number.The function t...
阅读全文
摘要: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 nu...
阅读全文
摘要:You are playing the following Flip Game with your friend: Given a string that contains only these two characters:+and-, you and your friend take turns...
阅读全文
摘要:You are playing the following Flip Game with your friend: Given a string that contains only these two characters:+and-, you and your friend take turns...
阅读全文
摘要: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 ston...
阅读全文
摘要:An abbreviation of a word follows the form . Below are some examples of word abbreviations:a) it --> it (no abbreviation) ...
阅读全文
摘要:Given an unsorted arraynums, reorder itin-placesuch thatnums[0] = nums[2] = nums[i - 1];Ifiis even, thennums[i] nums[i-1])){ swap(nums...
阅读全文
摘要:Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, wh...
阅读全文
摘要:Given a string, we can "shift" each of its letter to its successive letter, for example:"abc" -> "bcd". We can keep "shifting" which forms the sequenc...
阅读全文
摘要: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"].解题思路:直观思路...
阅读全文
摘要:Description:Count the number of prime numbers less than a non-negative number,n.解题思路:神一样的提示一步步给出了最佳解法。The Sieve of Eratosthenes uses an extra O(n) mem...
阅读全文
摘要:Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.The input string does not contain lead...
阅读全文
摘要:Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".解题思路:用trim() 和splite("\\s+")把S...
阅读全文
摘要:Rotate an array ofnelements to the right byksteps.For example, withn= 7 andk= 3, the array[1,2,3,4,5,6,7]is rotated to[5,6,7,1,2,3,4].Note:Try to come...
阅读全文
摘要:Given an array of integers, every element appearsthreetimes except for one. Find that single one.Note:Your algorithm should have a linear runtime comp...
阅读全文
摘要:Given an array of numbersnums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements t...
阅读全文
摘要:Given an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity...
阅读全文
摘要:Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.解题思路:解题想法是,从haystack的第一个位置...
阅读全文
摘要:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is read off as"tw...
阅读全文
摘要:There is a fence with n posts, each post can be painted with one of the k colors.You have to paint all the posts such that no more than two adjacent f...
阅读全文
摘要:Given two stringssandt, determine if they are isomorphic.Two strings are isomorphic if the characters inscan be replaced to gett.All occurrences of a ...
阅读全文
摘要: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...
阅读全文
摘要:Determine if a Sudoku is valid, according to:Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with ...
阅读全文
摘要:Design and implement a TwoSum class. It should support the following operations:addandfind.add- Add the number to an internal data structure.find- Fin...
阅读全文
摘要:The API:int read4(char *buf)reads 4 characters at a time from a file.The return value is the actual number of characters read. For example, it returns...
阅读全文
摘要:Given an integern, return the number of trailing zeroes inn!.Note:Your solution should be in logarithmic time complexity.解题思路:O(logn)解法:考虑n!的质数因子。后缀0总...
阅读全文
摘要:Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree{3,9,2...
阅读全文
摘要:Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For exa...
阅读全文
摘要: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 yo...
阅读全文
摘要:Given apatternand a stringstr, find ifstrfollows the same pattern.Examples:pattern ="abba", str ="dog cat cat dog"should return true.pattern ="abba", ...
阅读全文
摘要:A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).Write a function to determine if a number is...
阅读全文
摘要:Given an array of meeting time intervals consisting of start and end times[[s1,e1],[s2,e2],...](si= Ei, if true, continue compare, if false, return fa...
阅读全文
摘要:Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Note:Could you optimize your algorithm to use onlyO(...
阅读全文
摘要:GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]...
阅读全文
摘要:Given an integer array of sizen, find all elements that appear more than⌊ n/3 ⌋times. The algorithm should run in linear time and in O(1) space.Hint:H...
阅读全文
摘要:Given an array of sizen, find the majority element. The majority element is the element that appears more than⌊ n/2 ⌋times.You may assume that the arr...
阅读全文