Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhr5 committed Apr 19, 2019
1 parent 94fa798 commit 0a524db
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
31 changes: 31 additions & 0 deletions LeetCode/005. Longest Palindromic Substring.md
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;`
`}`
`};`
10 changes: 10 additions & 0 deletions LeetCode/709. To Lower Case.cpp
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;
}
};
14 changes: 14 additions & 0 deletions LeetCode/804. Unique Morse Code Words.cpp
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();
}
};
19 changes: 19 additions & 0 deletions LeetCode/929. Unique Email Addresses.cpp
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();
}
};

0 comments on commit 0a524db

Please sign in to comment.