Skip to content

Commit ec01186

Browse files
committed
Added Leetcode easy problems
1 parent 6a30f74 commit ec01186

File tree

4 files changed

+199
-0
lines changed

4 files changed

+199
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
# EXCEL SHEET COLUMN TITLE
3+
4+
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
5+
6+
For example:
7+
8+
1 -> A
9+
2 -> B
10+
3 -> C
11+
...
12+
26 -> Z
13+
27 -> AA
14+
28 -> AB
15+
...
16+
Example 1:
17+
18+
Input: 1
19+
Output: "A"
20+
21+
Example 2:
22+
23+
Input: 28
24+
Output: "AB"
25+
26+
Example 3:
27+
28+
Input: 701
29+
Output: "ZY"
30+
"""
31+
32+
class Solution:
33+
def convertToTitle(self, n: int) -> str:
34+
if n == 0:
35+
return ""
36+
37+
res = ""
38+
while n > 0:
39+
rem = n % 26
40+
if rem == 0:
41+
rem = 26
42+
res = chr(64+rem) + res
43+
n = n // 26
44+
if rem == 26:
45+
n -= 1
46+
47+
return res
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
# INTERSECTION OF TWO LINKED LISTS
3+
4+
Write a program to find the node at which the intersection of two singly linked lists begins.
5+
6+
For example, the following two linked lists:
7+
8+
begin to intersect at node c1.
9+
10+
Example 1:
11+
12+
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
13+
Output: Reference of the node with value = 8
14+
Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
15+
16+
Example 2:
17+
18+
Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
19+
Output: Reference of the node with value = 2
20+
Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
21+
22+
Example 3:
23+
24+
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
25+
Output: null
26+
Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
27+
Explanation: The two lists do not intersect, so return null.
28+
29+
Notes:
30+
31+
If the two linked lists have no intersection at all, return null.
32+
The linked lists must retain their original structure after the function returns.
33+
You may assume there are no cycles anywhere in the entire linked structure.
34+
Each value on each linked list is in the range [1, 10^9].
35+
Your code should preferably run in O(n) time and use only O(1) memory.
36+
"""
37+
38+
# Definition for singly-linked list.
39+
class ListNode:
40+
def __init__(self, x):
41+
self.val = x
42+
self.next = None
43+
44+
class Solution:
45+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
46+
cur1=headA
47+
cur2=headB
48+
while cur1!=cur2:
49+
if cur1==None:
50+
cur1=headB
51+
else:
52+
cur1=cur1.next
53+
if cur2==None:
54+
cur2=headA
55+
else:
56+
cur2=cur2.next
57+
return cur1

Leetcode/easy/majority-element.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
# MAJORITY ELEMENT
3+
4+
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
5+
6+
You may assume that the array is non-empty and the majority element always exist in the array.
7+
8+
Example 1:
9+
10+
Input: [3,2,3]
11+
Output: 3
12+
13+
Example 2:
14+
15+
Input: [2,2,1,1,1,2,2]
16+
Output: 2
17+
"""
18+
19+
class Solution:
20+
def majorityElement(self, nums) -> int:
21+
22+
candidate = nums[0]
23+
count = 0
24+
25+
for x in nums:
26+
if candidate == x:
27+
count += 1
28+
29+
else:
30+
if count != 0:
31+
count -= 1
32+
else:
33+
candidate = x
34+
count = 0
35+
36+
return candidate

crobot.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
class CRobot(object):
3+
def __init__(self):
4+
self.typeRobot = ""
5+
self.SN = 0
6+
self.orientation = 1
7+
self.status = False
8+
9+
def __init__(self, typeRobot, sn):
10+
self.typeRobot = typeRobot
11+
self.SN = sn
12+
self.orientation = 1
13+
self.status = False
14+
15+
def getType(self):
16+
return self.typeRobot
17+
18+
def getSN(self):
19+
return self.SN
20+
21+
def getOrientation(self):
22+
return self.orientation
23+
24+
def getState(self):
25+
return self.status
26+
27+
def setOrientation(self, x):
28+
self.orientation = x
29+
30+
def setState(self, x):
31+
self.status = x
32+
33+
def turn(self):
34+
if self.orientation == 1:
35+
self.orientation = 4
36+
else:
37+
self.orientation -= 1
38+
39+
def display(self):
40+
print("Serial number :",self.SN)
41+
print("State :",self.status)
42+
print("Orientation :",self.orientation)
43+
print("Type :",self.typeRobot)
44+
45+
46+
list_robot = []
47+
for i in range(1,5):
48+
robot = CRobot("X-Man Robot", i)
49+
list_robot.append(robot)
50+
51+
for i in range(0, 4):
52+
list_robot[i].display()
53+
list_robot[i].setState(True)
54+
list_robot[i].setOrientation(i+1)
55+
print("\n---\nUpdate of parameters\n---\n")
56+
for i in range(0,4):
57+
list_robot[i].display()
58+
59+

0 commit comments

Comments
 (0)