Skip to content

Commit 443bf8a

Browse files
committed
new cases MoveZeros and ValidPalindrom - easy litcode
1 parent e27e30e commit 443bf8a

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

InterviewAlgorithms/MoveZeros.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
2+
#
3+
# Note that you must do this in-place without making a copy of the array.
4+
5+
# Example 1:
6+
#
7+
# Input: nums = [0,1,0,3,12]
8+
# Output: [1,3,12,0,0]
9+
#
10+
# Example 2:
11+
#
12+
# Input: nums = [0]
13+
# Output: [0]
14+
15+
from typing import List
16+
17+
def moveZeroes(nums: List[int]) -> None:
18+
"""
19+
Do not return anything, modify nums in-place instead.
20+
"""
21+
length = len(nums)
22+
first_ind = 0
23+
second_ind = first_ind + 1
24+
while second_ind < length:
25+
left_val = nums[first_ind]
26+
right_val = nums[second_ind]
27+
# обновляем оба индекса так как их значения не нулевые.
28+
if (left_val != 0 and right_val != 0) or (left_val !=0 and right_val == 0):
29+
first_ind += 1
30+
second_ind += 1
31+
# обновляем только правый индекс.
32+
elif left_val == 0 and right_val == 0:
33+
second_ind += 1
34+
# swap values
35+
else:
36+
nums[first_ind], nums[second_ind] = nums[second_ind], nums[first_ind]
37+
print(nums)
38+
39+
40+
if __name__ == "__main__":
41+
# https: // leetcode.com / problems / move - zeroes / description /
42+
moveZeroes([0,1,0,3,12])
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters,
2+
# it reads the same forward and backward. Alphanumeric characters include letters and numbers.
3+
#
4+
# Given a string s, return true if it is a palindrome, or false otherwise.
5+
6+
# Example 1:
7+
#
8+
# Input: s = "A man, a plan, a canal: Panama"
9+
# Output: true
10+
11+
# Explanation: "amanaplanacanalpanama" is a palindrome.
12+
13+
# Example 2:
14+
#
15+
# Input: s = "race a car"
16+
# Output: false
17+
# Explanation: "raceacar" is not a palindrome.
18+
#
19+
# Constraints:
20+
#
21+
# 1 <= s.length <= 2 * 105
22+
# s consists only of printable ASCII characters.
23+
24+
def isPalindrome(s: str) -> bool:
25+
condition = True
26+
s = s.lower().strip()
27+
s = [letter for letter in s if letter.isalnum()]
28+
if s == 1:
29+
return condition
30+
else:
31+
index_left = 0
32+
index_right = len(s) - 1
33+
while index_left < index_right and condition:
34+
condition = s[index_left] == s[index_right]
35+
if condition:
36+
index_left += 1
37+
index_right -= 1
38+
else:
39+
break
40+
return condition
41+
42+
if __name__ == "__main__":
43+
print(isPalindrome("ab!#ba"))
44+

0 commit comments

Comments
 (0)