Crack Leetcode 1

Hello world! I try to write down every progress I’ve made from now on. Start with Leetcode. By acceptance order, I will provide my thinking and answer.

#771 Jewels and Stones(C++ version)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int numJewelsInStones(string J, string S) {
int count=0;
for(int i=0;i<J.size();i++){
for(int j=0;j<S.size();j++){
if(J[i]==S[j]){
count++;
}
}
}
return count;
}
};

#938 Range Sum of BST (C++ version)

I used recursion method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int rangeSumBST(TreeNode* root, int L, int R) {
int count = 0;
if(root==NULL)
return 0;
if(root->val >= L && root->val <=R)
count += root->val;
if(root->val >= L){
count += rangeSumBST(root->left,L,R);
}
if(root->val <= R){
count += rangeSumBST(root->right,L,R);
}

return count;
}
};

#807 Max Increase to Keep City Skyline(C++)

What I think is for every grid, the value equals min(the max value in the column,the max value in that line)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public:
int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
vector<int> grid_top;
vector<int> grid_left;
int count = 0;
for(int i=0;i<grid[0].size();i++){
grid_left.push_back(findMax(grid[i]));
int max = grid[0][i];
for(int j=0;j<grid.size();j++){
if(grid[j][i] >= max)
max = grid[j][i];
}
grid_top.push_back(max);
}

for(int i=0;i<grid.size();i++){
for(int j=0;j<grid[0].size();j++){
int temp = min(grid_left[j],grid_top[i]);
count += temp - grid[i][j];
}
}
return count;
}
int findMax(vector<int> list){
int max = list[0];
for(int i=1;i<list.size();i++){
if(list[i] >= max)
max = list[i];
}
return max;
}
};

#929 Unique Email Addresses(C++ version)

I realized the split function is reeeealy useful, maybe it’s time to switch to Java or Python :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public:
int numUniqueEmails(vector<string>& emails) {
vector<string> newEmails;
for(int i=0;i<emails.size();i++){
string email = emails[i];
string email_temp;
int plus_flag = 0;
int at_flag = 0;
for(int j=0;j<email.length();j++){
if(email[j] == '+')
plus_flag = 1;

if(!plus_flag){
if(email[j] != '.'){
email_temp = email_temp + email[j];
}
}
if(email[j] == '@'){
at_flag = 1;
}
if(at_flag){
email_temp= email_temp + email[j];
}
}
int same = 0;
for(int i=0;i<newEmails.size();i++){
if(newEmails[i] == email_temp)
same = 1;
}
if(!same)
newEmails.push_back(email_temp);

}
return newEmails.size();
}
};

#709. To Lower Case(Yes Java version)

It is about Ascii

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public String toLowerCase(String str) {
String result = "";
for(int i=0;i<str.length();i++){
int temp = str.charAt(i);
int ascii = temp;
if(temp <= 90 &&temp >= 65){
char r= (char) (temp + 32);
result += r;
}else{
result += str.charAt(i);
}
}
return result;
}
}

#804 Unique Morse Code Words

First find the morse code for each word, then duplicate checking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

class Solution {
public int uniqueMorseRepresentations(String[] words) {
String[] secrets = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
Set<String> set = new HashSet<String>();
for(String word:words){
String secretWord = "";
for(int i=0;i<word.length();i++) {
secretWord += secrets[(int)word.charAt(i)-97];
}
set.add(secretWord);
}
return set.size();
}
}