Skip to content

Commit 234e667

Browse files
authored
Added tasks 137-191
1 parent 5fe2997 commit 234e667

File tree

32 files changed

+833
-0
lines changed

32 files changed

+833
-0
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Large diffs are not rendered by default.
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package s0137_single_number_ii
2+
3+
// #Medium #Array #Bit_Manipulation #Top_Interview_150_Bit_Manipulation
4+
// #2025_05_21_Time_0_ms_(100.00%)_Space_5.16_MB_(75.27%)
5+
6+
func singleNumber(nums []int) int {
7+
ones := 0
8+
twos := 0
9+
for _, num := range nums {
10+
ones = (ones ^ num) & (^twos)
11+
twos = (twos ^ num) & (^ones)
12+
}
13+
return ones
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package s0137_single_number_ii
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestSingleNumber(t *testing.T) {
9+
assert.Equal(t, 3, singleNumber([]int{2, 2, 3, 2}))
10+
}
11+
12+
func TestSingleNumber2(t *testing.T) {
13+
assert.Equal(t, 99, singleNumber([]int{0, 1, 0, 1, 0, 1, 99}))
14+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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:** ((10 \* (6 / ((9 + 3) \* -11))) + 17) + 5
36+
37+
= ((10 \* (6 / (12 \* -11))) + 17) + 5
38+
39+
= ((10 \* (6 / -132)) + 17) + 5
40+
41+
= ((10 \* 0) + 17) + 5
42+
43+
= (0 + 17) + 5
44+
45+
= 17 + 5
46+
47+
= 22
48+
49+
**Constraints:**
50+
51+
* <code>1 <= tokens.length <= 10<sup>4</sup></code>
52+
* `tokens[i]` is either an operator: `"+"`, `"-"`, `"*"`, or `"/"`, or an integer in the range `[-200, 200]`.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package s0150_evaluate_reverse_polish_notation
2+
3+
// #Medium #Top_Interview_Questions #Array #Math #Stack #Programming_Skills_II_Day_3
4+
// #Top_Interview_150_Stack #2025_05_21_Time_0_ms_(100.00%)_Space_6.10_MB_(53.08%)
5+
6+
func evalRPN(tokens []string) int {
7+
stack := make([]int, 0)
8+
for _, token := range tokens {
9+
if len(token) == 1 && !isDigit(token[0]) {
10+
second := stack[len(stack)-1]
11+
first := stack[len(stack)-2]
12+
stack = stack[:len(stack)-2]
13+
stack = append(stack, eval(first, second, token))
14+
} else {
15+
num := 0
16+
sign := 1
17+
i := 0
18+
if token[0] == '-' {
19+
sign = -1
20+
i = 1
21+
}
22+
for ; i < len(token); i++ {
23+
num = num*10 + int(token[i]-'0')
24+
}
25+
stack = append(stack, num*sign)
26+
}
27+
}
28+
return stack[0]
29+
}
30+
31+
func eval(first, second int, operator string) int {
32+
switch operator {
33+
case "+":
34+
return first + second
35+
case "-":
36+
return first - second
37+
case "*":
38+
return first * second
39+
default:
40+
return first / second
41+
}
42+
}
43+
44+
func isDigit(c byte) bool {
45+
return c >= '0' && c <= '9'
46+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package s0150_evaluate_reverse_polish_notation
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestEvalRPN(t *testing.T) {
9+
assert.Equal(t, 9, evalRPN([]string{"2", "1", "+", "3", "*"}))
10+
}
11+
12+
func TestEvalRPN2(t *testing.T) {
13+
assert.Equal(t, 6, evalRPN([]string{"4", "13", "5", "/", "+"}))
14+
}
15+
16+
func TestEvalRPN3(t *testing.T) {
17+
assert.Equal(t, 22, evalRPN([]string{"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"}))
18+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
151\. Reverse Words in a String
2+
3+
Medium
4+
5+
Given an input string `s`, reverse the order of the **words**.
6+
7+
A **word** is defined as a sequence of non-space characters. The **words** in `s` will be separated by at least one space.
8+
9+
Return _a string of the words in reverse order concatenated by a single space._
10+
11+
**Note** that `s` may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "the sky is blue"
16+
17+
**Output:** "blue is sky the"
18+
19+
**Example 2:**
20+
21+
**Input:** s = " hello world "
22+
23+
**Output:** "world hello"
24+
25+
**Explanation:** Your reversed string should not contain leading or trailing spaces.
26+
27+
**Example 3:**
28+
29+
**Input:** s = "a good example"
30+
31+
**Output:** "example good a"
32+
33+
**Explanation:** You need to reduce multiple spaces between two words to a single space in the reversed string.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= s.length <= 10<sup>4</sup></code>
38+
* `s` contains English letters (upper-case and lower-case), digits, and spaces `' '`.
39+
* There is **at least one** word in `s`.
40+
41+
**Follow-up:** If the string data type is mutable in your language, can you solve it **in-place** with `O(1)` extra space?
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package s0151_reverse_words_in_a_string
2+
3+
// #Medium #String #Two_Pointers #LeetCode_75_Array/String #Udemy_Strings
4+
// #Top_Interview_150_Array/String #2025_05_21_Time_0_ms_(100.00%)_Space_5.37_MB_(51.24%)
5+
6+
func reverseWords(s string) string {
7+
var result []rune
8+
i := len(s) - 1
9+
for i >= 0 {
10+
if s[i] == ' ' {
11+
i--
12+
continue
13+
}
14+
start := i
15+
for start >= 0 && s[start] != ' ' {
16+
start--
17+
}
18+
if len(result) > 0 {
19+
result = append(result, ' ')
20+
}
21+
result = append(result, []rune(s[start+1:i+1])...)
22+
i = start - 1
23+
}
24+
return string(result)
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package s0151_reverse_words_in_a_string
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestReverseWords(t *testing.T) {
9+
assert.Equal(t, "blue is sky the", reverseWords("the sky is blue"))
10+
}
11+
12+
func TestReverseWords2(t *testing.T) {
13+
assert.Equal(t, "world hello", reverseWords(" hello world "))
14+
}
15+
16+
func TestReverseWords3(t *testing.T) {
17+
assert.Equal(t, "example good a", reverseWords("a good example"))
18+
}

0 commit comments

Comments
 (0)