Skip to content

Commit 83dfe3a

Browse files
committed
🚀 10-Aug-2020
1 parent 362c0ee commit 83dfe3a

16 files changed

+863
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
temporary/
1+
temporary/
2+
Notes.md
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
A boomerang is a set of 3 points that are all distinct and not in a straight line.
2+
3+
Given a list of three points in the plane, return whether these points are a boomerang.
4+
5+
6+
7+
Example 1:
8+
9+
Input: [[1,1],[2,3],[3,2]]
10+
Output: true
11+
Example 2:
12+
13+
Input: [[1,1],[2,2],[3,3]]
14+
Output: false
15+
16+
17+
Note:
18+
19+
points.length == 3
20+
points[i].length == 2
21+
0 <= points[i][j] <= 100
22+
23+
24+
25+
26+
27+
28+
29+
30+
// Using Herons Formula
31+
// If area of triangle is zero, => collinear
32+
33+
class Solution {
34+
public:
35+
bool isBoomerang(vector<vector<int>>& points) {
36+
int x1=points[0][0];
37+
int y1=points[0][1];
38+
int x2=points[1][0];
39+
int y2=points[1][1];
40+
int x3=points[2][0];
41+
int y3=points[2][1];
42+
43+
double a = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
44+
double b = sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
45+
double c = sqrt((x1-x3)*(x1-x3)+(y3-y1)*(y3-y1));
46+
47+
double s = (a+b+c)/2;
48+
49+
double area = sqrt(s*(s-a)*(s-b)*(s-c));
50+
51+
return area ? true: false;
52+
53+
}
54+
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
2+
3+
Symbol Value
4+
I 1
5+
V 5
6+
X 10
7+
L 50
8+
C 100
9+
D 500
10+
M 1000
11+
For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
12+
13+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
14+
15+
I can be placed before V (5) and X (10) to make 4 and 9.
16+
X can be placed before L (50) and C (100) to make 40 and 90.
17+
C can be placed before D (500) and M (1000) to make 400 and 900.
18+
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
19+
20+
Example 1:
21+
22+
Input: 3
23+
Output: "III"
24+
Example 2:
25+
26+
Input: 4
27+
Output: "IV"
28+
Example 3:
29+
30+
Input: 9
31+
Output: "IX"
32+
Example 4:
33+
34+
Input: 58
35+
Output: "LVIII"
36+
Explanation: L = 50, V = 5, III = 3.
37+
Example 5:
38+
39+
Input: 1994
40+
Output: "MCMXCIV"
41+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
class Solution {
53+
public:
54+
string intToRoman(int num) {
55+
vector<string> roman={"M","CM","D","CD","C","XC","L","XL","X","IX","V", "IV","I"};
56+
vector<int> no= {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
57+
string res;
58+
int i=0;
59+
while(num!=0){
60+
while(num>=no[i]){
61+
num-=no[i];
62+
res+=roman[i];
63+
}
64+
i++;
65+
}
66+
return res;
67+
}
68+
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
2+
3+
Symbol Value
4+
I 1
5+
V 5
6+
X 10
7+
L 50
8+
C 100
9+
D 500
10+
M 1000
11+
For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
12+
13+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
14+
15+
I can be placed before V (5) and X (10) to make 4 and 9.
16+
X can be placed before L (50) and C (100) to make 40 and 90.
17+
C can be placed before D (500) and M (1000) to make 400 and 900.
18+
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
19+
20+
Example 1:
21+
22+
Input: "III"
23+
Output: 3
24+
Example 2:
25+
26+
Input: "IV"
27+
Output: 4
28+
Example 3:
29+
30+
Input: "IX"
31+
Output: 9
32+
Example 4:
33+
34+
Input: "LVIII"
35+
Output: 58
36+
Explanation: L = 50, V= 5, III = 3.
37+
Example 5:
38+
39+
Input: "MCMXCIV"
40+
Output: 1994
41+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
class Solution {
56+
public:
57+
int romanToInt(string s) {
58+
int res=0;
59+
int n=s.length();
60+
unordered_map<char, int> mp;
61+
mp.insert({'I', 1});
62+
mp.insert({'V', 5});
63+
mp.insert({'X', 10});
64+
mp.insert({'L', 50});
65+
mp.insert({'C', 100});
66+
mp.insert({'D', 500});
67+
mp.insert({'M', 1000});
68+
69+
for(int i=0;i<n-1;i++){
70+
if(mp[s[i]]>=mp[s[i+1]]) res+=mp[s[i]];
71+
else res+=-mp[s[i]];
72+
}
73+
res+=mp[s[n-1]];
74+
75+
return res;
76+
}
77+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Given a column title as appear in an Excel sheet, return its corresponding column number.
2+
3+
For example:
4+
5+
A -> 1
6+
B -> 2
7+
C -> 3
8+
...
9+
Z -> 26
10+
AA -> 27
11+
AB -> 28
12+
...
13+
Example 1:
14+
15+
Input: "A"
16+
Output: 1
17+
Example 2:
18+
19+
Input: "AB"
20+
Output: 28
21+
Example 3:
22+
23+
Input: "ZY"
24+
Output: 701
25+
26+
27+
Constraints:
28+
29+
1 <= s.length <= 7
30+
s consists only of uppercase English letters.
31+
s is between "A" and "FXSHRXW".
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
class Solution {
43+
public:
44+
int titleToNumber(string s) {
45+
int res=0, k=0;
46+
int n=s.length();
47+
for(int i=n-1;i>=0;i--){
48+
int ch=(s[i]-'A')+1;
49+
res+=ch*pow(26,k++);
50+
}
51+
return res;
52+
}
53+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Count the number of prime numbers less than a non-negative number, n.
2+
3+
Example:
4+
5+
Input: 10
6+
Output: 4
7+
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
8+
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
class Solution {
21+
public:
22+
int countPrimes(int n) {
23+
bool seive[n+1];
24+
fill(seive, seive+n+1, true);
25+
for(int p=2; p*p<=n;p++){
26+
if(seive[p]==true){
27+
for(int i=p*p;i<=n;i+=p){
28+
seive[i]=false;
29+
}
30+
}
31+
}
32+
33+
int cnt=0;
34+
for(int p=2;p<n;p++){ // prime less than n, not including n
35+
if(seive[p]==true) cnt++;
36+
37+
}
38+
return cnt;
39+
}
40+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Given an integer, write a function to determine if it is a power of three.
2+
3+
Example 1:
4+
5+
Input: 27
6+
Output: true
7+
Example 2:
8+
9+
Input: 0
10+
Output: false
11+
Example 3:
12+
13+
Input: 9
14+
Output: true
15+
Example 4:
16+
17+
Input: 45
18+
Output: false
19+
Follow up:
20+
Could you do it without using any loop / recursion?
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
class Solution {
36+
public:
37+
bool isPowerOfThree(int n) {
38+
if(n<=0) return false;
39+
double res = log10(n)/log10(3);
40+
return res==int(res);
41+
}
42+
};

0 commit comments

Comments
 (0)