字符串的题目可能在笔试或面试中出现,包括一般原地操作的C风格的char类型字符串,以及一些容器的使用,还会涉及到atoi,split等函数的使用。
Leetcode 49. Group Anagrams
Medium
1855119FavoriteShare
Given an array of strings, group anagrams together.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"]
,
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Note:
- All inputs will be in lowercase.
- The order of your output does not matter.
这到题目是典型的Hash题目,Hash的映射关系是string, vector<string>
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
unordered_map<string, vector<string>> mp;
for(auto s:strs){
string temp = s;
sort(temp.begin(),temp.end());
mp[temp].push_back(s);
}
for(auto x:mp){
res.push_back(x.second);
}
return res;
}
};
165. Compare Version Numbers
Medium
3191145FavoriteShare
Compare two version numbers version1 and version2.
If version1 > version2
return 1;
if version1 < version2
return -1;
otherwise return 0
.
You may assume that the version strings are non-empty and contain only digits and the .
character.
The .
character does not represent a decimal point and is used to separate number sequences.
For instance, 2.5
is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
You may assume the default revision number for each level of a version number to be 0
. For example, version number 3.4
has a revision number of 3
and 4
for its first and second level revision number. Its third and fourth level revision number are both 0
.
Example 1:
Input:version1
= "0.1",version2
= "1.1" Output: -1
Example 2:
Input:version1
= "1.0.1",version2
= "1" Output: 1
Example 3:
Input:version1
= "7.5.2.4",version2
= "7.5.3"