Skip to content

Commit a78ef1f

Browse files
committed
🚀 02-Aug-2020
1 parent 11cb37e commit a78ef1f

File tree

4 files changed

+259
-0
lines changed

4 files changed

+259
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Given a word, you need to judge whether the usage of capitals in it is right or not.
2+
3+
We define the usage of capitals in a word to be right when one of the following cases holds:
4+
5+
All letters in this word are capitals, like "USA".
6+
All letters in this word are not capitals, like "leetcode".
7+
Only the first letter in this word is capital, like "Google".
8+
Otherwise, we define that this word doesn't use capitals in a right way.
9+
10+
11+
Example 1:
12+
13+
Input: "USA"
14+
Output: True
15+
16+
17+
Example 2:
18+
19+
Input: "FlaG"
20+
Output: False
21+
22+
23+
Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
class Solution {
38+
public:
39+
bool detectCapitalUse(string word) {
40+
int n=word.length();
41+
int sm=0, bg=0;
42+
for(int i=0;i<n;i++){
43+
if(word[i]>='a' && word[i]<='z') sm++;
44+
else bg++;
45+
}
46+
if(bg==n) return true;
47+
if(sm==n) return true;
48+
if((word[0]>='A' && word[0]<='Z') && sm==n-1) return true;
49+
return false;
50+
}
51+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
You are climbing a stair case. It takes n steps to reach to the top.
2+
3+
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
4+
5+
Example 1:
6+
7+
Input: 2
8+
Output: 2
9+
Explanation: There are two ways to climb to the top.
10+
1. 1 step + 1 step
11+
2. 2 steps
12+
Example 2:
13+
14+
Input: 3
15+
Output: 3
16+
Explanation: There are three ways to climb to the top.
17+
1. 1 step + 1 step + 1 step
18+
2. 1 step + 2 steps
19+
3. 2 steps + 1 step
20+
21+
22+
Constraints:
23+
24+
1 <= n <= 45
25+
Hide Hint #1
26+
To reach nth step, what could have been your previous steps? (Think about the step sizes)
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
// recursive (TLE)
40+
41+
class Solution {
42+
public:
43+
44+
int rec(int n){
45+
if(n<=1) return 1; // Only one way to reach
46+
return rec(n-1) + rec(n-2);
47+
}
48+
49+
int climbStairs(int n) {
50+
return rec(n);
51+
}
52+
};
53+
54+
55+
56+
57+
58+
59+
60+
// Memoization
61+
62+
class Solution {
63+
public:
64+
65+
int rec(int n, int dp[]){
66+
if(n<=1) return 1;
67+
if(dp[n]!=-1) return dp[n];
68+
dp[n-1]=rec(n-1, dp);
69+
dp[n-2]=rec(n-2, dp);
70+
dp[n]=dp[n-1]+dp[n-2];
71+
return dp[n];
72+
}
73+
74+
int climbStairs(int n) {
75+
int dp[n+1];
76+
fill(dp, dp+n+1, -1);
77+
return rec(n, dp);
78+
}
79+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Given a word, you need to judge whether the usage of capitals in it is right or not.
2+
3+
We define the usage of capitals in a word to be right when one of the following cases holds:
4+
5+
All letters in this word are capitals, like "USA".
6+
All letters in this word are not capitals, like "leetcode".
7+
Only the first letter in this word is capital, like "Google".
8+
Otherwise, we define that this word doesn't use capitals in a right way.
9+
10+
11+
Example 1:
12+
13+
Input: "USA"
14+
Output: True
15+
16+
17+
Example 2:
18+
19+
Input: "FlaG"
20+
Output: False
21+
22+
23+
Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
class Solution {
38+
public:
39+
bool detectCapitalUse(string word) {
40+
int n=word.length();
41+
int sm=0, bg=0;
42+
for(int i=0;i<n;i++){
43+
if(word[i]>='a' && word[i]<='z') sm++;
44+
else bg++;
45+
}
46+
if(bg==n) return true;
47+
if(sm==n) return true;
48+
if((word[0]>='A' && word[0]<='Z') && sm==n-1) return true;
49+
return false;
50+
}
51+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
You are climbing a stair case. It takes n steps to reach to the top.
2+
3+
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
4+
5+
Example 1:
6+
7+
Input: 2
8+
Output: 2
9+
Explanation: There are two ways to climb to the top.
10+
1. 1 step + 1 step
11+
2. 2 steps
12+
Example 2:
13+
14+
Input: 3
15+
Output: 3
16+
Explanation: There are three ways to climb to the top.
17+
1. 1 step + 1 step + 1 step
18+
2. 1 step + 2 steps
19+
3. 2 steps + 1 step
20+
21+
22+
Constraints:
23+
24+
1 <= n <= 45
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
// recursive (TLE)
39+
40+
class Solution {
41+
public:
42+
43+
int rec(int n){
44+
if(n<=1) return 1; // Only one way to reach
45+
return rec(n-1) + rec(n-2);
46+
}
47+
48+
int climbStairs(int n) {
49+
return rec(n);
50+
}
51+
};
52+
53+
54+
55+
56+
57+
58+
59+
// Memoization
60+
61+
class Solution {
62+
public:
63+
64+
int rec(int n, int dp[]){
65+
if(n<=1) return 1;
66+
if(dp[n]!=-1) return dp[n];
67+
dp[n-1]=rec(n-1, dp);
68+
dp[n-2]=rec(n-2, dp);
69+
dp[n]=dp[n-1]+dp[n-2];
70+
return dp[n];
71+
}
72+
73+
int climbStairs(int n) {
74+
int dp[n+1];
75+
fill(dp, dp+n+1, -1);
76+
return rec(n, dp);
77+
}
78+
};

0 commit comments

Comments
 (0)