Skip to content

Commit dd8de6a

Browse files
committed
first commit to LeetCode Repo.
1 parent 627906f commit dd8de6a

7 files changed

+171
-0
lines changed

344. Reverse String.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Solution one
2+
class Solution {
3+
public:
4+
void reverseString(vector<char>& s) {
5+
6+
7+
rec(s, 0, s.size() - 1);
8+
/*
9+
reverse(s.begin(), s.end());
10+
for(int i=0; i<s.size(); i++){
11+
if(i == 0) cout << "[\"" << s[i] << '"' << ",";
12+
else if(i == s.size()-1){
13+
cout << '"' << s[i] << '"' << "]" << endl;
14+
}
15+
else cout << '"' << s[i] << '"' << ",";
16+
}
17+
*/
18+
19+
}
20+
void rec(vector<char> &v, int st, int ed){
21+
if(st >= ed){
22+
return;
23+
}
24+
rec(v, st + 1, ed - 1);
25+
swap(v[st], v[ed]);
26+
}
27+
};
28+
29+
// Solution Two
30+
class Solution {
31+
public:
32+
void reverseString(vector<char>& s) {
33+
34+
reverse(s.begin(), s.end());
35+
for(int i=0; i<s.size(); i++){
36+
if(i == 0) cout << "[\"" << s[i] << '"' << ",";
37+
else if(i == s.size()-1){
38+
cout << '"' << s[i] << '"' << "]" << endl;
39+
}
40+
else cout << '"' << s[i] << '"' << ",";
41+
}
42+
43+
}
44+
};

415. Add Strings.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
string addStrings(string num1, string num2) {
4+
int s1 = num1.size(), s2 = num2.size();
5+
if(s1 < s2){
6+
while(s1 < s2){
7+
num1 = "0" + num1;
8+
s1++;
9+
}
10+
}
11+
if(s2 < s1){
12+
while(s2 < s1){
13+
num2 = "0" + num2;
14+
s2++;
15+
}
16+
}
17+
int carry = 0, a, b;
18+
string sum = "";
19+
for(int i=s1 - 1; i>=0; i--){
20+
a = num1[i] - '0';
21+
b = num2[i] - '0';
22+
if(carry + a + b >= 10){
23+
carry += a + b;
24+
sum += char((carry % 10) + '0');
25+
carry /= 10;
26+
}
27+
else{
28+
carry += a + b;
29+
sum += char((carry % 10) + '0');
30+
carry /= 10;
31+
}
32+
}
33+
while(carry){
34+
sum += char((carry % 10) + '0');
35+
carry /= 10;
36+
}
37+
reverse(sum.begin(), sum.end());
38+
return sum;
39+
}
40+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
string reverseWords(string s) {
4+
stringstream ss(s);
5+
string words;
6+
string str = "";
7+
while(ss >> words){
8+
reverse(words.begin(), words.end());
9+
str += words;
10+
str += " ";
11+
}
12+
str.pop_back();
13+
return str;
14+
}
15+
};

657. Robot Return to Origin.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
bool judgeCircle(string moves) {
4+
int r, c;
5+
r = c = 0;
6+
for(int i=0; i<moves.size(); i++){
7+
if(moves[i] == 'U') c++;
8+
else if(moves[i] == 'D') c--;
9+
else if(moves[i] == 'L') r++;
10+
else r--;
11+
}
12+
if(r == 0 && c == 0) return true;
13+
return false;
14+
}
15+
};

709. To Lower Case.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
string toLowerCase(string str) {
4+
for(int i=0; i<str.size(); i++){
5+
str[i] = tolower(str[i]);
6+
}
7+
return str;
8+
}
9+
};

804. Unique Morse Code Words.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int uniqueMorseRepresentations(vector<string>& words) {
4+
vector<string> s = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
5+
set<string> st;
6+
for(int i=0; i<words.size(); i++){
7+
string str = "";
8+
for(int j=0; j<words[i].size(); j++){
9+
str += s[words[i][j] - 'a'];
10+
}
11+
st.insert(str);
12+
}
13+
return st.size();
14+
}
15+
};

929. Unique Email Addresses.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
int numUniqueEmails(vector<string>& emails) {
4+
set<string> st;
5+
for(int i=0; i<emails.size(); i++){
6+
string str = "";
7+
bool plus = false, atThe = false;
8+
for(int j=0; j<emails[i].size(); j++){
9+
if(!plus){
10+
// if plus is not found yet
11+
if(emails[i][j] == '.' && !atThe) continue;
12+
else{
13+
if(emails[i][j] == '+' && !atThe){
14+
plus = true;
15+
}
16+
else str += emails[i][j];
17+
}
18+
}
19+
else{
20+
// if plus is found then find the @
21+
if(emails[i][j] == '@'){
22+
plus = false;
23+
atThe = true;
24+
str += '@';
25+
}
26+
}
27+
}
28+
//cout << str << endl;
29+
st.insert(str);
30+
}
31+
return st.size();
32+
}
33+
};

0 commit comments

Comments
 (0)