-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Solution { | ||
public: | ||
|
||
string longestPalindrome(string s) { | ||
if (s.length() < 1) return ""; | ||
int len1, len2, len,maxlen=0; | ||
int start = 0, end = 0; | ||
for (int i = 0; i < s.length(); i++) { | ||
len1 = expandAroundCenter(s, i, i); | ||
len2 = expandAroundCenter(s, i, i + 1); | ||
len = max(len1, len2); | ||
if (len > maxlen) maxlen = len; | ||
if (len > end - start) { | ||
start = i - (len - 1) / 2; | ||
end = i + len / 2; | ||
//cout << i << " " << start << " " << end << " " << len << endl; | ||
} | ||
} | ||
//cout << start << " " << end << " " << len << endl; | ||
return s.substr(start, maxlen);//参考的solution 结果Java的substring是[start,end),C++的string是(start,length)因为随着遍历len可能还会变小,所以留存最大的len | ||
} | ||
`private:` | ||
`int expandAroundCenter(string s,int left,int right){` | ||
`int L=left,R=right;` | ||
`while(L>=0&&R<s.length()&&s[L]==s[R]){` | ||
`L--;` | ||
`R++;` | ||
`}` | ||
`return R-L-1;` | ||
`}` | ||
`};` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Solution { | ||
public: | ||
//'a'-32='A' | ||
string toLowerCase(string str) { | ||
for (char& c : str) { | ||
if (c >= 'A' && c <= 'Z') c += 32; | ||
} | ||
return str; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution { | ||
string m[27]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."}; | ||
public: | ||
int uniqueMorseRepresentations(vector<string>& words) { | ||
vector<string> d = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}; | ||
unordered_set<string> s; | ||
for (auto word : words) { | ||
string code; | ||
for (auto c : word) code += d[c - 'a']; | ||
s.insert(code); | ||
} | ||
return s.size(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution { | ||
public: | ||
int numUniqueEmails(vector<string>& emails) { | ||
unordered_set<string> s; | ||
for(auto email:emails){ | ||
string str; | ||
auto it = email.begin(); | ||
for (; '@' != *it && '+' != *it; ++it) { | ||
if ('.' != *it) { | ||
str += *it; | ||
} | ||
} | ||
it=find(it, email.end(), '@'); | ||
str.append(it, email.end()); | ||
s.insert(str); | ||
} | ||
return s.size(); | ||
} | ||
}; |