Skip to content

Commit ac7b26b

Browse files
leetcode questions added
1 parent a27d5a2 commit ac7b26b

8 files changed

+248
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
3+
4+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
5+
6+
You can return the answer in any order.
7+
8+
Question: https://leetcode.com/problems/two-sum/
9+
10+
"""
11+
12+
class Solution:
13+
def twoSum(self, nums: List[int], target: int) -> List[int]:
14+
for i in range(len(nums)):
15+
for j in range(i+1, len(nums)):
16+
if nums[i] + nums[j] == target:
17+
return [i,j]
18+
return []
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Given an integer x, return true if x is palindrome integer.
3+
4+
An integer is a palindrome when it reads the same backward as forward.
5+
6+
For example, 121 is a palindrome while 123 is not.
7+
8+
Question: https://leetcode.com/problems/palindrome-number/
9+
10+
"""
11+
12+
13+
class Solution:
14+
def isPalindrome(self, x: int) -> bool:
15+
if x < 0:
16+
return False
17+
c = x
18+
b = 0
19+
20+
while c:
21+
b = b * 10 + c % 10
22+
c //= 10
23+
24+
return b == x
25+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
3+
4+
Symbol Value
5+
I 1
6+
V 5
7+
X 10
8+
L 50
9+
C 100
10+
D 500
11+
M 1000
12+
For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
13+
14+
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:
15+
16+
I can be placed before V (5) and X (10) to make 4 and 9.
17+
X can be placed before L (50) and C (100) to make 40 and 90.
18+
C can be placed before D (500) and M (1000) to make 400 and 900.
19+
Given a roman numeral, convert it to an integer.
20+
21+
Questions: https://leetcode.com/problems/roman-to-integer/
22+
23+
"""
24+
25+
26+
class Solution:
27+
def romanToInt(self, s: str) -> int:
28+
roman = {'I':1, 'V':5, 'X': 10, 'L':50, 'C':100, 'D':500, 'M':1000}
29+
30+
res = 0
31+
32+
for i in range(len(s)):
33+
if i+1 <len(s) and roman[s[i]] < roman[s[i+1]]:
34+
res = res - roman[s[i]]
35+
else:
36+
res = res + roman[s[i]]
37+
return res
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Write a function to find the longest common prefix string amongst an array of strings.
3+
4+
If there is no common prefix, return an empty string "".
5+
6+
Questions: https://leetcode.com/problems/longest-common-prefix/
7+
8+
"""
9+
10+
class Solution:
11+
def longestCommonPrefix(self, strs: List[str]) -> str:
12+
res = ""
13+
n = len(strs)
14+
strs.sort()
15+
first = strs[0]
16+
last = strs[n-1]
17+
for i in range(len(first)):
18+
if first[i] != last[i]:
19+
return res
20+
else:
21+
res = res + first[i]
22+
return res
23+
24+
25+
26+
# for i in range(len(strs[0])):
27+
# for s in strs:
28+
# if i == len(s) or s[i] != strs[0][i]:
29+
# return res
30+
31+
# res += strs[0][i]
32+
33+
# return res
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
3+
4+
An input string is valid if:
5+
6+
Open brackets must be closed by the same type of brackets.
7+
Open brackets must be closed in the correct order.
8+
9+
Question: https://leetcode.com/problems/valid-parentheses/
10+
11+
12+
"""
13+
14+
15+
class Solution:
16+
def isValid(self, s: str) -> bool:
17+
para = {')':'(', ']':'[', '}':'{'}
18+
op = ['(','[', '{']
19+
stack = []
20+
21+
for c in s:
22+
if c in op:
23+
stack.append(c)
24+
25+
elif c in para:
26+
if len(stack) != 0 and stack[-1] == para[c]:
27+
stack.pop()
28+
else:
29+
return False
30+
31+
if len(stack) == 0:
32+
return True
33+
else:
34+
return False
35+
36+
37+
38+
39+
40+
41+
# if c in para:
42+
# if len(stack) != 0 and stack[-1] == para[c]:
43+
# stack.pop()
44+
# else:
45+
# return False
46+
47+
# else:
48+
# stack.append(c)
49+
50+
# if len(stack) == 0:
51+
# return True
52+
# else:
53+
# False
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
You are given the heads of two sorted linked lists list1 and list2.
3+
4+
Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.
5+
6+
Return the head of the merged linked list.
7+
8+
Question: https://leetcode.com/problems/merge-two-sorted-lists/
9+
10+
"""
11+
12+
13+
class ListNode:
14+
def __init__(self, val=0, next=None):
15+
self.val = val
16+
self.next = next
17+
class Solution:
18+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
19+
answer = ListNode()
20+
tail = answer
21+
while list1 and list2:
22+
if list1.val < list2.val:
23+
tail.next = list1
24+
list1 = list1.next
25+
else:
26+
tail.next = list2
27+
list2=list2.next
28+
tail = tail.next
29+
30+
if list1:
31+
tail.next = list1
32+
elif list2:
33+
tail.next = list2
34+
35+
return answer.next
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.
3+
4+
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.
5+
6+
Return k after placing the final result in the first k slots of nums.
7+
8+
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
9+
10+
Question: https://leetcode.com/problems/remove-duplicates-from-sorted-array/
11+
12+
"""
13+
14+
class Solution:
15+
def removeDuplicates(self, nums: List[int]) -> int:
16+
list = 1
17+
18+
for r in range(1, len(nums)):
19+
if nums[r] != nums[r-1]:
20+
nums[list] = nums[r]
21+
list += 1
22+
23+
return list
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed.
3+
4+
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.
5+
6+
Return k after placing the final result in the first k slots of nums.
7+
8+
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
9+
10+
Question: https://leetcode.com/problems/remove-element/
11+
12+
"""
13+
14+
15+
class Solution:
16+
def removeDuplicates(self, nums: List[int]) -> int:
17+
list = 1
18+
19+
for r in range(1, len(nums)):
20+
if nums[r] != nums[r-1]:
21+
nums[list] = nums[r]
22+
list += 1
23+
24+
return list

0 commit comments

Comments
 (0)