Skip to content

Commit 670707c

Browse files
committed
first commit
0 parents  commit 670707c

11 files changed

+212
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def twoSum(self, numbers: List[int], target: int) -> List[int]:
3+
hash = {}
4+
for pos in range(len(numbers)):
5+
if target-numbers[pos] in hash:
6+
return [hash[target-numbers[pos]]+1, pos+1]
7+
else:
8+
hash[numbers[pos]] = pos

189. Rotate Array.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def rotate(self, nums: List[int], k: int) -> None:
3+
"""
4+
Do not return anything, modify nums in-place instead.
5+
"""
6+
ln, k = len(nums), k % len(nums)
7+
nums[:] = nums[ln-k:] + nums[0:ln-k]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
8+
node = head
9+
total = 0
10+
while node:
11+
total += 1
12+
node = node.next
13+
if total == 1:
14+
return None
15+
elif total == n:
16+
return head.next
17+
total = total - n
18+
node = head
19+
x=1
20+
while x< total:
21+
node= node.next
22+
x+=1
23+
node.next = node.next.next
24+
return head

283. Move Zeroes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def moveZeroes(self, nums: List[int]) -> None:
3+
"""
4+
Do not return anything, modify nums in-place instead.
5+
"""
6+
i=0
7+
ln = len(nums)
8+
while i < ln:
9+
if nums[i] == 0:
10+
nums.pop(i)
11+
nums.append(0)
12+
i-=1
13+
ln-=1
14+
i+=1
15+
return nums
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def lengthOfLongestSubstring(self, s: str) -> int:
3+
ln = len(s)
4+
max = 0
5+
for i in range(ln):
6+
hash = {}
7+
tmp = 0
8+
for j in range(i,ln):
9+
if s[j] not in hash:
10+
hash[s[j]] = s[j]
11+
tmp+=1
12+
else:
13+
break
14+
if tmp > max:
15+
max = tmp
16+
hash.clear()
17+
return max

344. Reverse String.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def reverseString(self, s: List[str]) -> None:
3+
"""
4+
Do not return anything, modify s in-place instead.
5+
"""
6+
ln = len(s)
7+
8+
for i in range(int(ln/2)):
9+
tmp = s[i]
10+
s[i] = s[ln-i-1]
11+
s[ln-i-1] = tmp

35. Search Insert Position.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def searchInsert(self, nums: List[int], target: int) -> int:
3+
for i,val in enumerate(nums):
4+
if target==val or target<val:
5+
return i
6+
return i+

567. Permutation in String.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution:
2+
# def checkInclusion(self, s1: str, s2: str) -> bool:
3+
# s1 = ''.join(sorted(s1))
4+
# l1 , l2= len(s1), len(s2)
5+
# r1, r2 = 0, l1
6+
7+
# while r2 <= l2:
8+
# if s1 == ''.join(sorted(s2[r1:r2])):
9+
# return True
10+
# r1+=1
11+
# r2+=1
12+
# return False
13+
#approach 2
14+
def checkInclusion(self, s1: str, s2: str) -> bool:
15+
hash1 = {}
16+
# for i in range(97,123):
17+
# hash1[chr(i)] = 0
18+
for i in s1:
19+
if i in hash1:
20+
hash1[i] += 1
21+
else:
22+
hash1[i] = 1
23+
# print(hash1)
24+
25+
l1 , l2= len(s1), len(s2)
26+
r1, r2 = 0, l1
27+
while r2 <= l2:
28+
hash2 = {}
29+
x = s2[r1 : r2]
30+
for i in range(r1,r2):
31+
32+
if s2[i] in hash2:
33+
hash2[s2[i]] += 1
34+
else:
35+
hash2[s2[i]] = 1
36+
c=0
37+
# for i in s1:
38+
# if i not in hash2.keys():
39+
# break
40+
# elif hash1[i] == hash2[i]:
41+
# c+=1
42+
# if c == l1:
43+
# return True
44+
# or simply
45+
if hash1==hash2:
46+
return True
47+
48+
r1+=1
49+
r2+=1
50+
return False

695. Max Area of Island.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Solution:
2+
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
3+
4+
# make all node unvisited at first
5+
r , n = len(grid), len(grid[0])
6+
visited=[]
7+
for i in range(r):
8+
t=[]
9+
for j in range(n):
10+
t.append(0)
11+
visited.append(t)
12+
13+
max = 0 # initialize maxirur island area = 0
14+
for i in range(r):
15+
for j in range(n):
16+
self.max = 0
17+
if grid[i][j] == 1 and visited[i][j] == 0: # check only land which is unvisited
18+
visited[i][j] = 1
19+
self.max += 1
20+
21+
def dfs(x,y):
22+
# check right node
23+
if y < n-1 :
24+
if grid[x][y+1] == 1 and visited[x][y+1] == 0:
25+
self.max += 1
26+
visited[x][y+1] = 1
27+
dfs(x, y+1)
28+
# check bottom node
29+
if x < r-1 :
30+
if grid[x+1][y] == 1 and visited[x+1][y] == 0:
31+
self.max += 1
32+
visited[x+1][y] = 1
33+
dfs(x+1,y)
34+
# check left node
35+
if y > 0 :
36+
if grid[x][y-1] == 1 and visited[x][y-1] == 0:
37+
self.max += 1
38+
visited[x][y-1] = 1
39+
dfs(x, y-1)
40+
# check top node
41+
if x > 0 :
42+
if grid[x-1][y] == 1 and visited[x-1][y] == 0:
43+
self.max += 1
44+
visited[x-1][y] = 1
45+
dfs(x-1, y)
46+
return self.max
47+
area = dfs(i,j)
48+
if area > max:
49+
max = area
50+
return max
51+
52+
53+

704. Binary Search.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def search(self, nums: List[int], target: int) -> int:
3+
end = len(nums)-1
4+
start = 0
5+
while(start<= end):
6+
mid = (start+end)//2
7+
if nums[mid] == target:
8+
return mid
9+
elif target >= nums[start] and target <= nums[mid]:
10+
end = mid
11+
elif target > nums[mid] and target <= nums[end]:
12+
start = mid+1
13+
else :
14+
return -1
15+

0 commit comments

Comments
 (0)