Find th Difference
Given two strings s
and t
which consist of only lowercase letters.
String t
is generated by random shuffling string s
and then add one more letter at a random position.
Find the letter that was added in t
.
Example:1
2
3
4
5
6
7
8
9Input:
s = "abcd"
t = "abcde"
Output:
e
Explanation:
'e' is the letter that was added.
1 | /** |
Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
Example 1:1
2Input: [1,3,4,2,2]
Output: 2
Example 2:1
2Input: [3,1,3,4,2]
Output: 3
`
Note:
- You must not modify the array (assume the array is read only).
- You must use only constant, O(1) extra space.
- Your runtime complexity should be less than O(n2).
- There is only one duplicate number in the array, but it could be repeated more than once.
1 | /** |
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 the previous version, all the versions after a bad version are also bad.
Suppose you have n versions [1, 2, …, n] and you want to find out the first bad one, which causes all the following ones to be bad.
You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
Example 1:1
2
3
4
5
6
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.
1 | /** |
First Missing Positive
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:1
2Input: [1,2,0]
Output: 3
Example 2:1
2Input: [3,4,-1,1]
Output: 2
Example 3:1
2Input: [7,8,9,11,12]
Output: 1
First Unique Character in a String
Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.
Examples:1
2
3
4
5s = "leetcode"
return 0.
s = "loveleetcode",
return 2.
1 | /** |