Leetcode - 007/ Numbers
65. Valid Number[H]
https://leetcode.com/problems/valid-number/
Description
Validate if a given string can be interpreted as a decimal number.
Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
" -90e3 " => true
" 1e" => false
"e3" => false
" 6e-1" => true
" 99e2.5 " => false
"53.5e93" => true
" --6 " => false
"-+3" => false
"95a54e53" => false
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:
- Numbers 0-9
- Exponent - “e”
- Positive/negative sign - “+”/"-"
- Decimal point - “.”
Of course, the context of these characters also matters in the input.
Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
Solution
1 | class Solution: |
1 | class Solution: |
136. Single Number[E]
https://leetcode.com/problems/single-number/
Description
Given a non-empty 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 without using extra memory?
Example 1:
1 | Input: [2,2,1] |
Example 2:
1 | Input: [4,1,2,1,2] |
Solution
https://leetcode.com/problems/single-number/solution/
1 | class Solution: |
137. Single Number II[M]
https://leetcode.com/problems/single-number-ii/
Description
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
1 | Input: [2,2,3,2] |
Example 2:
1 | Input: [0,1,0,1,0,1,99] |
Solution
1 | class Solution: |
260. Single Number III[M]
https://leetcode.com/problems/single-number-iii/
Description
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
Example:
1 | Input: [1,2,1,3,2,5] |
Note:
- The order of the result is not important. So in the above example,
[5, 3]is also correct. - Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
Solution
1 |
179. Largest Number[M]
https://leetcode.com/problems/largest-number/
Description
Given a list of non negative integers, arrange them such that they form the largest number.
Example 1:
1 | Input: [10,2] |
Example 2:
1 | Input: [3,30,34,5,9] |
Note: The result may be very large, so you need to return a string instead of an integer.
Solution
1 | class Solution: |
129. Sum Root to Leaf Numbers[M]
https://leetcode.com/problems/sum-root-to-leaf-numbers/
Description
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers.
Note: A leaf is a node with no children.
Example:
1 | Input: [1,2,3] |
Example 2:
1 | Input: [4,9,0,5,1] |
Solution
1 |
200. Number of Islands[M]
https://leetcode.com/problems/number-of-islands/
Description
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
1 | Input: grid = [ |
Example 2:
1 | Input: grid = [ |
Solution
1 |
202. Happy Number[E]
https://leetcode.com/problems/happy-number/
Description
Write an algorithm to determine if a number n 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 the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Return True if n is a happy number, and False if not.
Example:
1 | Input: 19 |
Solution
https://en.wikipedia.org/wiki/Happy_number
1 | class Solution: |
263. Ugly Number[E]
https://leetcode.com/problems/ugly-number/
Description
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.
Example 1:
1 | Input: 6 |
Example 2:
1 | Input: 8 |
Example 3:
1 | Input: 14 |
Note:
1is typically treated as an ugly number.- Input is within the 32-bit signed integer range: [−231, 231 − 1].
Solution
1 |
264. Ugly Number II[M]
https://leetcode.com/problems/ugly-number-ii/
Description
Write a program to find the n-th ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.
Example:
1 | Input: n = 10 |
Note:
1is typically treated as an ugly number.ndoes not exceed 1690.
Solution
1 |
268. Missing Number[E]
https://leetcode.com/problems/missing-number/
Description
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
Example 1:
1 | Input: [3,0,1] |
Example 2:
1 | Input: [9,6,4,2,3,5,7,0,1] |
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
Solution
1 | class Solution: |
374. Guess Number Higher or Lower[E]
https://leetcode.com/problems/guess-number-higher-or-lower/
Description
We are playing the Guess Game. The game is as follows:
I pick a number from 1 to *n*. You have to guess which number I picked.
Every time you guess wrong, I’ll tell you whether the number is higher or lower.
You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0):
1 | -1 : My number is lower |
Example :
1 | Input: n = 10, pick = 6 |
Solution
1 | # The guess API is already defined for you. |
375. Guess Number Higher or Lower II[E]
https://leetcode.com/problems/guess-number-higher-or-lower-ii/
Description
We are playing the Guess Game. The game is as follows:
I pick a number from 1 to n. You have to guess which number I picked.
Every time you guess wrong, I’ll tell you whether the number I picked is higher or lower.
However, when you guess a particular number x, and you guess wrong, you pay $x. You win the game when you guess the number I picked.
Example:
1 | n = 10, I pick 8. |
Given a particular n ≥ 1, find out how much money you need to have to guarantee a win.
Solution
1 |
414. Third Maximum Number[E]
https://leetcode.com/problems/third-maximum-number/
Description
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
Example 1:
1 | Input: [3, 2, 1] |
Example 2:
1 | Input: [1, 2] |
Example 3:
1 | Input: [2, 2, 3, 1] |
Solution
1 |
421. Maximum XOR of Two Numbers in an Array[M]
https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/
Description
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231.
Find the maximum result of ai XOR aj, where 0 ≤ i, j < n.
Could you do this in O(n) runtime?
Example:
1 | Input: [3, 10, 5, 25, 2, 8] |
Solution
1 |
434. Number of Segments in a String[E]
https://leetcode.com/problems/number-of-segments-in-a-string/
Description
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
Please note that the string does not contain any non-printable characters.
Example:
1 | Input: "Hello, my name is John" |
Solution
https://leetcode.com/problems/number-of-segments-in-a-string/solution/
1 | class Solution: |
448. Find All Numbers Disappeared in an Array[E]
https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/
Description
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements of [1, n] inclusive that do not appear in this array.
Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
Example:
1 | Input: |
Solution
1 | class Solution: |
628. Maximum Product of Three Numbers[E]
https://leetcode.com/problems/maximum-product-of-three-numbers/
Description
Given an integer array, find three numbers whose product is maximum and output the maximum product.
Example 1:
1 | Input: [1,2,3] |
Example 2:
1 | Input: [1,2,3,4] |
Note:
- The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
- Multiplication of any three numbers in the input won’t exceed the range of 32-bit signed integer.
Solution
https://leetcode.com/problems/maximum-product-of-three-numbers/solution/
1 | class Solution: |
933. Number of Recent Calls[E]
https://leetcode.com/problems/number-of-recent-calls/
Description
Write a class RecentCounter to count recent requests.
It has only one method: ping(int t), where t represents some time in milliseconds.
Return the number of pings that have been made from 3000 milliseconds ago until now.
Any ping with time in [t - 3000, t] will count, including the current ping.
It is guaranteed that every call to ping uses a strictly larger value of t than before.
Example 1:
1 | Input: inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]] |
Note:
- Each test case will have at most
10000calls toping. - Each test case will call
pingwith strictly increasing values oft. - Each call to ping will have
1 <= t <= 10^9.
Solution
1 |





