Given aĀ sortedĀ (in ascending order) integer arrayĀ numsĀ ofĀ nĀ elements and aĀ targetĀ value, write a function to searchĀ targetĀ inĀ nums. IfĀ targetĀ exists, then return its index, otherwise returnĀ -1.
Example 1:
Input:nums = [-1,0,3,5,9,12], target = 9 Output: 4 Explanation: 9 exists in nums and its index is 4
Example 2:
Input:nums = [-1,0,3,5,9,12], target = 2 Output: -1 Explanation: 2 does not exist in nums so return -1
Note:
You may assume that all elements inĀ numsĀ are unique.
nĀ will be in the rangeĀ [1, 10000].
The value of each element inĀ numsĀ will be in the rangeĀ [-9999, 9999].
Solution: Binary Search
Time complexity: O(logn)
Space complexity: O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Author: Huahua
// Running time: 32 ms
classSolution{
public:
intsearch(vector<int>& nums, int target) {
int l = 0;
intr=nums.size();
while(l<r){
intm=(r-l)/2+l;
if(nums[m]==target)
returnm;
elseif(nums[m]>target)
r=m;
else
l=m+1;
}
return-1;
}
};
STL
1
2
3
4
5
6
7
8
9
10
// Author: Huahua
// Running time: 36 ms
classSolution{
public:
intsearch(vector<int>& nums, int target) {
auto it = lower_bound(nums.begin(), nums.end(), target);
We are given a 2-dimensionalĀ grid.Ā "."Ā is an empty cell,Ā "#"Ā isĀ a wall,Ā "@"Ā is the starting point, ("a",Ā "b", …) are keys, and ("A",Ā "B", …) are locks.
We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.Ā We cannot walk outside the grid, or walk into a wall.Ā If we walk over a key, we pick it up.Ā We can’t walk over a lock unless we have the corresponding key.
For someĀ 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the firstĀ KĀ letters of the English alphabet in the grid.Ā This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks wereĀ chosen in the same order as the English alphabet.
Return the lowest number of moves to acquire all keys.Ā IfĀ it’s impossible, returnĀ -1.
Two stringsĀ XĀ andĀ YĀ are similar if we can swap two letters (in different positions) ofĀ X, so thatĀ it equalsĀ Y.
For example,Ā "tars"Ā andĀ "rats"Ā are similar (swapping at positionsĀ 0Ā andĀ 2), andĀ "rats"Ā andĀ "arts"Ā are similar, butĀ "star"Ā is not similar toĀ "tars",Ā "rats", orĀ "arts".
Together, these form two connected groups by similarity:Ā {"tars", "rats", "arts"}Ā andĀ {"star"}.Ā Notice thatĀ "tars"Ā andĀ "arts"Ā are in the same group even though they are not similar.Ā Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group.
We are given a listĀ AĀ of strings.Ā Every string inĀ AĀ is an anagram of every other string inĀ A.Ā How many groups are there?
Example 1:
Input: ["tars","rats","arts","star"]
Output: 2
Note:
A.length <= 2000
A[i].length <= 1000
A.length * A[i].length <= 20000
All words inĀ AĀ consist of lowercase letters only.
All words inĀ AĀ have the same length and are anagrams of each other.
The judging time limit has been increased for this question.