65. Valid Number
Validate if a given string is numeric.
Some examples:
"0"
=> true
" 0.1 "
=> true
"abc"
=> false
"1 a"
=> false
"2e10"
=> true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
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: 直接分情况讨论即可,这题的难点在于情况太多。
除此之外,解法还可以有有限向量机、正则匹配。
Code:
class Solution {
public:
bool isNumber(string s) {
//去除首尾空格
int i=0;
int t=s.size()-1;
while(s[i]==' ') i++;
while(s[t]==' ') t--;
//跳过正负号
if(s[i]=='+' || s[i]=='-') i++;
//判断是否不可能存在数字部分
if(i>t) return false;
//标记'.'和'e'的位置
int ip = -1;//'.'
int ie = -1;//'e'
bool hasnum = false;
for(int j=i; j<=t; j++){
if(s[j]<'0' || s[j]>'9'){
if(s[j]=='.'){
if(ip<0) ip = j;
else return false; //有效数字不可能同时存在两个以上的'.'
}else if(s[j]=='e'){
if(ie<0){
ie = j;
if(s[ie+1]=='+' || s[ie+1]=='-'){
s.erase(ie+1,1);
t--;
}
}
else return false; //有效数字不可能同时存在两个以上的'e'
}else{
return false;//出现了非合法字符(非数字、非'.'、非'e')
}
}else{
hasnum = true;
}
}
if(ie<0) return hasnum;
else{
//存在'e'
if(ip<0){
//不存在'.','e'前后都要有数字
if(ie>0 && (s[ie-1]>='0' && s[ie-1]<='9') && (s[ie+1]>='0' && s[ie+1]<='9')) return true;
else return false;
}else{
//存在'.','e'前后都要有数字
if(ie<ip) return false;//'.'要在'e'的前面,否则非法
else{
if(s[ie+1]<'0' || s[ie+1]>'9') return false;//'e'后必须要有数字
else{
//'.'前或后存在数字都合法,否则非法
if(s[ip+1]>='0' && s[ip+1]<='9') return true;
else if(ip>0 && s[ip-1]>='0' && s[ip-1]<='9') return true;
else return false;
}
}
}
}
return false;
}
};
12. Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Solution: 使用罗马数字的基本型,贪心可得最终解。
Code:
class Solution {
public:
string intToRoman(int num) {
//I=1,V=5,X=10,L=50,C=100,D=500,M=1000
//但根据这几位数字,还能组合出两位的基本型,加起来一共有13种
//{4, 9, 40, 90, 400, 900},{"IV", "IX", "XL", "XC", "CD", "CM"}
int vInt[13] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
string vRom[13] = {"I","IV", "V","IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
string ans = "";
int i;
while(num>0){
for(i=12; i>=0; i--){
if(vInt[i]<=num) break;
}
ans += vRom[i];
num -= vInt[i];
}
return ans;
}
};
49. Group Anagrams
Given an array of strings, group anagrams together.
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
,
Return:
[ ["ate", "eat","tea"], ["nat","tan"], ["bat"] ]
Note: All inputs will be in lower-case.
Solution: 判断是否为anagrams的基本思路就是sort之后看看是否相等。对于这一题,可以利用unordered_map来记录sort后的值,这样就不需要每次对比的时候都重复排序。Code:
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> m;
for(int i=0; i<strs.size(); i++){
string key = strs[i];
sort(key.begin(),key.end());
m[key].push_back(strs[i]);
}
vector<vector<string>> ans;
//遍历unordered_map
for(auto i=m.cbegin(); i!=m.cend(); i++){
ans.push_back(i->second);
sort(ans[ans.size()-1].begin(), ans[ans.size()-1].end());
}
return ans;
}
};