Skip to content

Commit edba2c8

Browse files
ThanhNITjavadev
authored andcommitted
Added tasks 137-160
1 parent 74e6a58 commit edba2c8

File tree

16 files changed

+371
-0
lines changed

16 files changed

+371
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
388388
| 0005 |[Longest Palindromic Substring](src/main/python/g0001_0100/s0005_longest_palindromic_substring/Solution0005.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 47 | 97.38
389389
| 0394 |[Decode String](src/main/python/g0301_0400/s0394_decode_string/Solution0394.py)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, LeetCode_75_Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
390390
| 0049 |[Group Anagrams](src/main/python/g0001_0100/s0049_group_anagrams/Solution0049.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 11 | 82.75
391+
| 0151 |[Reverse Words in a String](src/main/python/g0101_0200/s0151_reverse_words_in_a_string/Solution0151.py)| Medium | String, Two_Pointers, LeetCode_75_Array/String | 0 | 100.00
391392

392393
#### Udemy Binary Search
393394

@@ -552,6 +553,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
552553
| 0012 |[Integer to Roman](src/main/python/g0001_0100/s0012_integer_to_roman/Solution0012.py)| Medium | String, Hash_Table, Math, Big_O_Time_O(1)_Space_O(1) | 0 | 100.00
553554
| 0058 |[Length of Last Word](src/main/python/g0001_0100/s0058_length_of_last_word/Solution0058.py)| Easy | String | 0 | 100.00
554555
| 0014 |[Longest Common Prefix](src/main/python/g0001_0100/s0014_longest_common_prefix/Solution0014.py)| Easy | Top_Interview_Questions, String, Big_O_Time_O(n\*m)_Space_O(m) | 0 | 100.00
556+
| 0151 |[Reverse Words in a String](src/main/python/g0101_0200/s0151_reverse_words_in_a_string/Solution0151.py)| Medium | String, Two_Pointers, LeetCode_75_Array/String | 0 | 100.00
555557
| 0006 |[Zigzag Conversion](src/main/python/g0001_0100/s0006_zigzag_conversion/Solution0006.py)| Medium | String, Big_O_Time_O(n)_Space_O(n) | 5 | 93.09
556558
| 0028 |[Implement strStr()](src/main/python/g0001_0100/s0028_find_the_index_of_the_first_occurrence_in_a_string/Solution0028.py)| Easy | Top_Interview_Questions, String, Two_Pointers, String_Matching | 0 | 100.00
557559
| 0068 |[Text Justification](src/main/python/g0001_0100/s0068_text_justification/Solution0068.py)| Hard | Array, String, Simulation, 2025_09_13_Time_0_ms_(100.00%)_Space_17.85_MB_(62.97%) | |
@@ -561,6 +563,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
561563
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
562564
|-|-|-|-|-|-
563565
| 0125 |[Valid Palindrome](src/main/python/g0101_0200/s0125_valid_palindrome/Solution0125.py)| Hard | Array, Dynamic_Programming | 232 | 75.22
566+
| 0167 |[Two Sum II - Input Array Is Sorted](src/main/python/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/Solution0167.py)| Medium | Array, Binary_Search, Two_Pointers | 3 | 80.89
564567
| 0011 |[Container With Most Water](src/main/python/g0001_0100/s0011_container_with_most_water/Solution0011.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Greedy, Two_Pointers, LeetCode_75_Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 68 | 91.16
565568
| 0015 |[3Sum](src/main/python/g0001_0100/s0015_3sum/Solution0015.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 581 | 56.92
566569

@@ -603,6 +606,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
603606
| 0020 |[Valid Parentheses](src/main/python/g0001_0100/s0020_valid_parentheses/Solution0020.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
604607
| 0071 |[Simplify Path](src/main/python/g0001_0100/s0071_simplify_path/Solution0071.py)| Medium | String, Stack | 0 | 100.00
605608
| 0155 |[Min Stack](src/main/python/g0101_0200/s0155_min_stack/MinStack.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Big_O_Time_O(1)_Space_O(N) | 3 | 83.83
609+
| 0150 |[Evaluate Reverse Polish Notation](src/main/python/g0101_0200/s0150_evaluate_reverse_polish_notation/Solution0150.py)| Medium | Top_Interview_Questions, Array, Math, Stack | 0 | 100.00
606610

607611
#### Top Interview 150 Linked List
608612

@@ -703,6 +707,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
703707
|-|-|-|-|-|-
704708
| 0035 |[Search Insert Position](src/main/python/g0001_0100/s0035_search_insert_position/Solution0035.py)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
705709
| 0074 |[Search a 2D Matrix](src/main/python/g0001_0100/s0074_search_a_2d_matrix/Solution0074.py)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 0 | 100.00
710+
| 0162 |[Find Peak Element](src/main/python/g0101_0200/s0162_find_peak_element/Solution0162.py)| Medium | Top_Interview_Questions, Array, Binary_Search, LeetCode_75_Binary_Search | 0 | 100.00
706711
| 0033 |[Search in Rotated Sorted Array](src/main/python/g0001_0100/s0033_search_in_rotated_sorted_array/Solution0033.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
707712
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/python/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution0034.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
708713
| 0153 |[Find Minimum in Rotated Sorted Array](src/main/python/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution0153.py)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 0 | 100.00
@@ -721,6 +726,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
721726
|-|-|-|-|-|-
722727
| 0067 |[Add Binary](src/main/python/g0001_0100/s0067_add_binary/Solution0067.py)| Easy | String, Math, Bit_Manipulation, Simulation | 0 | 100.00
723728
| 0136 |[Single Number](src/main/python/g0101_0200/s0136_single_number/Solution0136.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, LeetCode_75_Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
729+
| 0137 |[Single Number II](src/main/python/g0101_0200/s0137_single_number_ii/Solution0137.py)| Medium | Array, Bit_Manipulation | 0 | 100.00
724730

725731
#### Top Interview 150 Math
726732

@@ -993,6 +999,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
993999
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9941000
|-|-|-|-|-|-
9951001
| 0283 |[Move Zeroes](src/main/python/g0201_0300/s0283_move_zeroes/Solution0283.py)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, LeetCode_75_Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 3 | 80.05
1002+
| 0167 |[Two Sum II - Input Array Is Sorted](src/main/python/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/Solution0167.py)| Medium | Array, Binary_Search, Two_Pointers | 3 | 80.89
9961003

9971004
#### Day 4 Two Pointers
9981005

@@ -1074,6 +1081,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
10741081
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10751082
|-|-|-|-|-|-
10761083
| 0153 |[Find Minimum in Rotated Sorted Array](src/main/python/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution0153.py)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 0 | 100.00
1084+
| 0162 |[Find Peak Element](src/main/python/g0101_0200/s0162_find_peak_element/Solution0162.py)| Medium | Top_Interview_Questions, Array, Binary_Search, LeetCode_75_Binary_Search | 0 | 100.00
10771085

10781086
#### Day 3 Two Pointers
10791087

@@ -1229,6 +1237,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
12291237

12301238
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12311239
|-|-|-|-|-|-
1240+
| 0167 |[Two Sum II - Input Array Is Sorted](src/main/python/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/Solution0167.py)| Medium | Array, Binary_Search, Two_Pointers | 3 | 80.89
12321241

12331242
#### Day 8
12341243

@@ -1322,6 +1331,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
13221331

13231332
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13241333
|-|-|-|-|-|-
1334+
| 0162 |[Find Peak Element](src/main/python/g0101_0200/s0162_find_peak_element/Solution0162.py)| Medium | Top_Interview_Questions, Array, Binary_Search, LeetCode_75_Binary_Search | 0 | 100.00
13251335

13261336
#### Day 13
13271337

@@ -1572,6 +1582,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
15721582

15731583
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15741584
|-|-|-|-|-|-
1585+
| 0150 |[Evaluate Reverse Polish Notation](src/main/python/g0101_0200/s0150_evaluate_reverse_polish_notation/Solution0150.py)| Medium | Top_Interview_Questions, Array, Math, Stack | 0 | 100.00
15751586
| 0066 |[Plus One](src/main/python/g0001_0100/s0066_plus_one/Solution0066.py)| Easy | Top_Interview_Questions, Array, Math | 0 | 100.00
15761587

15771588
#### Day 4
@@ -1708,16 +1719,21 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
17081719
| 0198 |[House Robber](src/main/python/g0101_0200/s0198_house_robber/Solution0198.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, LeetCode_75_DP/1D, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_3, Level_2_Day_12_Dynamic_Programming, Udemy_Dynamic_Programming, Top_Interview_150_1D_DP, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
17091720
| 0189 |[Rotate Array](src/main/python/g0101_0200/s0189_rotate_array/Solution0189.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Algorithm_I_Day_2_Two_Pointers, Udemy_Arrays, Top_Interview_150_Array/String, Big_O_Time_O(n)_Space_O(1) | 3 | 74.65
17101721
| 0169 |[Majority Element](src/main/python/g0101_0200/s0169_majority_element/Solution0169.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Data_Structure_II_Day_1_Array, Udemy_Famous_Algorithm, Top_Interview_150_Array/String, Big_O_Time_O(n)_Space_O(1) | 7 | 56.72
1722+
| 0167 |[Two Sum II - Input Array Is Sorted](src/main/python/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/Solution0167.py)| Medium | Array, Binary_Search, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Binary_Search_I_Day_7, Top_Interview_150_Two_Pointers | 3 | 80.89
1723+
| 0162 |[Find Peak Element](src/main/python/g0101_0200/s0162_find_peak_element/Solution0162.py)| Medium | Top_Interview_Questions, Array, Binary_Search, LeetCode_75_Binary_Search, Algorithm_II_Day_2_Binary_Search, Binary_Search_II_Day_12, Top_Interview_150_Binary_Search | 0 | 100.00
17111724
| 0160 |[Intersection of Two Linked Lists](src/main/python/g0101_0200/s0160_intersection_of_two_linked_lists/Solution0160.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_II_Day_11_Linked_List, Udemy_Linked_List, Big_O_Time_O(M+N)_Space_O(1) | 83 | 67.29
17121725
| 0155 |[Min Stack](src/main/python/g0101_0200/s0155_min_stack/MinStack.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Data_Structure_II_Day_14_Stack_Queue, Programming_Skills_II_Day_18, Level_2_Day_16_Design, Udemy_Design, Top_Interview_150_Stack, Big_O_Time_O(1)_Space_O(N) | 3 | 83.83
17131726
| 0153 |[Find Minimum in Rotated Sorted Array](src/main/python/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution0153.py)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Algorithm_II_Day_2_Binary_Search, Binary_Search_I_Day_12, Udemy_Binary_Search, Top_Interview_150_Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 0 | 100.00
17141727
| 0152 |[Maximum Product Subarray](src/main/python/g0101_0200/s0152_maximum_product_subarray/Solution0152.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Dynamic_Programming_I_Day_6, Level_2_Day_13_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 3 | 98.51
1728+
| 0151 |[Reverse Words in a String](src/main/python/g0101_0200/s0151_reverse_words_in_a_string/Solution0151.py)| Medium | String, Two_Pointers, LeetCode_75_Array/String, Udemy_Strings, Top_Interview_150_Array/String | 0 | 100.00
1729+
| 0150 |[Evaluate Reverse Polish Notation](src/main/python/g0101_0200/s0150_evaluate_reverse_polish_notation/Solution0150.py)| Medium | Top_Interview_Questions, Array, Math, Stack, Programming_Skills_II_Day_3, Top_Interview_150_Stack | 0 | 100.00
17151730
| 0148 |[Sort List](src/main/python/g0101_0200/s0148_sort_list/Solution0148.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Linked_List, Divide_and_Conquer, Merge_Sort, Level_2_Day_4_Linked_List, Top_Interview_150_Divide_and_Conquer, Big_O_Time_O(log(N))_Space_O(log(N)) | 186 | 43.67
17161731
| 0146 |[LRU Cache](src/main/python/g0101_0200/s0146_lru_cache/LRUCache.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Udemy_Linked_List, Top_Interview_150_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 113 | 84.08
17171732
| 0142 |[Linked List Cycle II](src/main/python/g0101_0200/s0142_linked_list_cycle_ii/Solution0142.py)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_II_Day_10_Linked_List, Level_1_Day_4_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 38 | 94.15
17181733
| 0141 |[Linked List Cycle](src/main/python/g0101_0200/s0141_linked_list_cycle/Solution0141.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_I_Day_7_Linked_List, Udemy_Linked_List, Top_Interview_150_Linked_List, Big_O_Time_O(N)_Space_O(1) | 44 | 80.06
17191734
| 0139 |[Word Break](src/main/python/g0101_0200/s0139_word_break/Solution0139.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Algorithm_II_Day_15_Dynamic_Programming, Dynamic_Programming_I_Day_9, Udemy_Dynamic_Programming, Top_Interview_150_1D_DP, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 3 | 70.59
17201735
| 0138 |[Copy List with Random Pointer](src/main/python/g0101_0200/s0138_copy_list_with_random_pointer/Solution0138.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Programming_Skills_II_Day_14, Udemy_Linked_List, Top_Interview_150_Linked_List, Big_O_Time_O(N)_Space_O(N) | 38 | 77.31
1736+
| 0137 |[Single Number II](src/main/python/g0101_0200/s0137_single_number_ii/Solution0137.py)| Medium | Array, Bit_Manipulation, Top_Interview_150_Bit_Manipulation | 0 | 100.00
17211737
| 0136 |[Single Number](src/main/python/g0101_0200/s0136_single_number/Solution0136.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, LeetCode_75_Bit_Manipulation, Data_Structure_II_Day_1_Array, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Integers, Top_Interview_150_Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
17221738
| 0135 |[Candy](src/main/python/g0101_0200/s0135_candy/Solution0135.py)| Hard | Array, Greedy, Top_Interview_150_Array/String | 7 | 97.91
17231739
| 0134 |[Gas Station](src/main/python/g0101_0200/s0134_gas_station/Solution0134.py)| Medium | Top_Interview_Questions, Array, Greedy, Top_Interview_150_Array/String | 15 | 88.80
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# #Medium #Array #Bit_Manipulation #Top_Interview_150_Bit_Manipulation
2+
# #2025_09_14_Time_0_ms_(100.00%)_Space_19.05_MB_(88.35%)
3+
4+
from typing import List
5+
6+
class Solution:
7+
def singleNumber(self, nums: List[int]) -> int:
8+
ones = 0
9+
twos = 0
10+
for num in nums:
11+
ones = (ones ^ num) & (~twos)
12+
twos = (twos ^ num) & (~ones)
13+
return ones
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import unittest
2+
from Solution0137 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_singleNumber(self):
6+
self.assertEqual(Solution().singleNumber([2, 2, 3, 2]), 3)
7+
8+
def test_singleNumber2(self):
9+
self.assertEqual(Solution().singleNumber([0, 1, 0, 1, 0, 1, 99]), 99)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
137\. Single Number II
2+
3+
Medium
4+
5+
Given an integer array `nums` where every element appears **three times** except for one, which appears **exactly once**. _Find the single element and return it_.
6+
7+
You must implement a solution with a linear runtime complexity and use only constant extra space.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [2,2,3,2]
12+
13+
**Output:** 3
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [0,1,0,1,0,1,99]
18+
19+
**Output:** 99
20+
21+
**Constraints:**
22+
23+
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
24+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
25+
* Each element in `nums` appears exactly **three times** except for one element which appears **once**.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# #Medium #Top_Interview_Questions #Array #Math #Stack #Programming_Skills_II_Day_3
2+
# #Top_Interview_150_Stack #2025_09_14_Time_0_ms_(100.00%)_Space_18.88_MB_(99.67%)
3+
4+
from typing import List
5+
6+
class Solution:
7+
def evalRPN(self, tokens: List[str]) -> int:
8+
stack = []
9+
10+
for token in tokens:
11+
if token in {'+', '-', '*', '/'}:
12+
b = stack.pop()
13+
a = stack.pop()
14+
if token == '+':
15+
stack.append(a + b)
16+
elif token == '-':
17+
stack.append(a - b)
18+
elif token == '*':
19+
stack.append(a * b)
20+
elif token == '/':
21+
# Truncate toward zero
22+
stack.append(int(a / b))
23+
else:
24+
stack.append(int(token))
25+
26+
return stack[0]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from Solution0150 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_evalRPN(self):
6+
self.assertEqual(Solution().evalRPN(["2", "1", "+", "3", "*"]), 9)
7+
8+
def test_evalRPN2(self):
9+
self.assertEqual(Solution().evalRPN(["4", "13", "5", "/", "+"]), 6)
10+
11+
def test_evalRPN3(self):
12+
self.assertEqual(
13+
Solution().evalRPN(
14+
["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
15+
),
16+
22
17+
)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
150\. Evaluate Reverse Polish Notation
2+
3+
Medium
4+
5+
Evaluate the value of an arithmetic expression in [Reverse Polish Notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation).
6+
7+
Valid operators are `+`, `-`, `*`, and `/`. Each operand may be an integer or another expression.
8+
9+
**Note** that division between two integers should truncate toward zero.
10+
11+
It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.
12+
13+
**Example 1:**
14+
15+
**Input:** tokens = ["2","1","+","3","\*"]
16+
17+
**Output:** 9
18+
19+
**Explanation:** ((2 + 1) \* 3) = 9
20+
21+
**Example 2:**
22+
23+
**Input:** tokens = ["4","13","5","/","+"]
24+
25+
**Output:** 6
26+
27+
**Explanation:** (4 + (13 / 5)) = 6
28+
29+
**Example 3:**
30+
31+
**Input:** tokens = ["10","6","9","3","+","-11","\*","/","\*","17","+","5","+"]
32+
33+
**Output:** 22
34+
35+
**Explanation:**
36+
37+
((10 \* (6 / ((9 + 3) \* -11))) + 17) + 5
38+
= ((10 \* (6 / (12 \* -11))) + 17) + 5
39+
= ((10 \* (6 / -132)) + 17) + 5
40+
= ((10 \* 0) + 17) + 5
41+
= (0 + 17) + 5
42+
= 17 + 5
43+
= 22
44+
45+
**Constraints:**
46+
47+
* <code>1 <= tokens.length <= 10<sup>4</sup></code>
48+
* `tokens[i]` is either an operator: `"+"`, `"-"`, `"*"`, or `"/"`, or an integer in the range `[-200, 200]`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# #Medium #String #Two_Pointers #LeetCode_75_Array/String #Udemy_Strings
2+
# #Top_Interview_150_Array/String #2025_09_14_Time_0_ms_(100.00%)_Space_17.89_MB_(69.02%)
3+
4+
class Solution:
5+
def reverseWords(self, s: str) -> str:
6+
result = []
7+
i = len(s) - 1
8+
while i >= 0:
9+
if s[i] == ' ':
10+
i -= 1
11+
continue
12+
start = s.rfind(' ', 0, i)
13+
result.append(' ')
14+
result.append(s[start + 1:i + 1])
15+
i = start - 1
16+
if result:
17+
result.pop(0) # Remove the leading space
18+
return ''.join(result)

0 commit comments

Comments
 (0)