Skip to content

Commit 767705d

Browse files
authored
Create arraylist.py
1 parent 4c4c035 commit 767705d

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

arraylist.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class ArrayListManager:
2+
'''
3+
* @param n: You should generate an array list of n elements.
4+
* @return: The array list your just created.
5+
'''
6+
def create(self, n):
7+
# Write your code here
8+
self.arr = [i for i in range(n)]
9+
return self.arr
10+
11+
12+
'''
13+
* @param list: The list you need to clone
14+
* @return: A deep copyed array list from the given list
15+
'''
16+
def clone(self, list):
17+
# Write your code here
18+
return [i for i in list]
19+
20+
21+
'''
22+
* @param list: The array list to find the kth element
23+
* @param k: Find the kth element
24+
* @return: The kth element
25+
'''
26+
def get(self, list, k):
27+
# Write your code here
28+
return list[k]
29+
30+
31+
'''
32+
* @param list: The array list
33+
* @param k: Find the kth element, set it to val
34+
* @param val: Find the kth element, set it to val
35+
'''
36+
def set(self, list, k, val):
37+
# write your code here
38+
list[k] = val
39+
40+
41+
'''
42+
* @param list: The array list to remove the kth element
43+
* @param k: Remove the kth element
44+
'''
45+
def remove(self, list, k):
46+
# write tour code here
47+
for i in range(k, len(list) - 1):
48+
list[i] = list[i + 1]
49+
list.pop()
50+
51+
52+
'''
53+
* @param list: The array list.
54+
* @param val: Get the index of the first element that equals to val
55+
* @return: Return the index of that element
56+
'''
57+
def indexOf(self, list, val):
58+
# Write your code here
59+
for i in range(len(list)):
60+
if list[i] == val:
61+
return i
62+
return -1
63+
64+
# easy: https://www.lintcode.com/problem/385/

0 commit comments

Comments
 (0)