Skip to content

Commit c942e0b

Browse files
committedDec 24, 2020
Added Leetcode easy problems
1 parent ec01186 commit c942e0b

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed
 

‎.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "C:\\Users\\Subhan\\AppData\\Local\\Microsoft\\WindowsApps\\python.exe"
3+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
# EXCEL SHEET COLUMN NUMBER
3+
4+
Given a column title as appear in an Excel sheet, return its corresponding column number.
5+
6+
For example:
7+
8+
A -> 1
9+
B -> 2
10+
C -> 3
11+
...
12+
Z -> 26
13+
AA -> 27
14+
AB -> 28
15+
...
16+
Example 1:
17+
18+
Input: "A"
19+
Output: 1
20+
21+
Example 2:
22+
23+
Input: "AB"
24+
Output: 28
25+
26+
Example 3:
27+
28+
Input: "ZY"
29+
Output: 701
30+
31+
Constraints:
32+
33+
1 <= s.length <= 7
34+
s consists only of uppercase English letters.
35+
s is between "A" and "FXSHRXW".
36+
"""
37+
38+
class Solution:
39+
def titleToNumber(self, s: str) -> int:
40+
k = 0
41+
res = 0
42+
while s:
43+
temp = ord(s[-1]) - 64
44+
res += temp * 26 ** k
45+
k += 1
46+
s = s[:-1]
47+
48+
return res
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
# FACTORIAL TRAILING ZEROS
3+
4+
Given an integer n, return the number of trailing zeroes in n!.
5+
6+
Follow up: Could you write a solution that works in logarithmic time complexity?
7+
8+
Example 1:
9+
10+
Input: n = 3
11+
Output: 0
12+
Explanation: 3! = 6, no trailing zero.
13+
14+
Example 2:
15+
16+
Input: n = 5
17+
Output: 1
18+
Explanation: 5! = 120, one trailing zero.
19+
20+
Example 3:
21+
22+
Input: n = 0
23+
Output: 0
24+
25+
Constraints:
26+
27+
0 <= n <= 104
28+
"""
29+
30+
class Solution:
31+
def trailingZeroes(self, n: int) -> int:
32+
c = 0
33+
while n >= 5:
34+
c += n // 5
35+
n = n // 5
36+
37+
return c

‎Leetcode/easy/reverse-bits.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
# REVERSE BITS
3+
4+
Reverse bits of a given 32 bits unsigned integer.
5+
6+
Note:
7+
8+
Note that in some languages such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
9+
In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 2 above, the input represents the signed integer -3 and the output represents the signed integer -1073741825.
10+
Follow up:
11+
12+
If this function is called many times, how would you optimize it?
13+
14+
Example 1:
15+
16+
Input: n = 00000010100101000001111010011100
17+
Output: 964176192 (00111001011110000010100101000000)
18+
Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.
19+
Example 2:
20+
21+
Input: n = 11111111111111111111111111111101
22+
Output: 3221225471 (10111111111111111111111111111111)
23+
Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.
24+
25+
Constraints:
26+
27+
The input must be a binary string of length 32
28+
"""
29+
30+
class Solution:
31+
def reverseBits(self, n: int) -> int:
32+
l = 1 << 31
33+
r = 1
34+
35+
# Continue until the bits from each bitmask meet each other.
36+
while l > r:
37+
# Check if current bits from the left and right are 1 or 0.
38+
l_bit = 1 if n & l else 0
39+
r_bit = 1 if n & r else 0
40+
41+
# If two bits are different, we need to inverse them.
42+
if l_bit != r_bit:
43+
# Invert the bits in original number usin XOR.
44+
n = n ^ l ^ r
45+
46+
# Shiftin left bitmask to the right and the right one to the left.
47+
l = l >> 1
48+
r = r << 1
49+
50+
return n

‎test-panda.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
import pandas as pd
3+
4+
# Define a dictionary containing employee data
5+
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
6+
'Age':[27, 24, 22, 32],
7+
'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
8+
'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
9+
10+
# Convert the dictionary into DataFrame
11+
df = pd.DataFrame(data)
12+
13+
# select all rows
14+
# and second to fourth column
15+
df[df.columns[1:4]]

0 commit comments

Comments
 (0)
Please sign in to comment.