Skip to content

Commit 1eca4f5

Browse files
authored
Added tasks 128-152
1 parent 8117957 commit 1eca4f5

File tree

32 files changed

+1092
-0
lines changed

32 files changed

+1092
-0
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
137137

138138
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
139139
|-|-|-|-|-|-
140+
| 0139 |[Word Break](src/main/scala/g0101_0200/s0139_word_break/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 482 | 66.67
140141

141142
#### Day 16 Dynamic Programming
142143

@@ -372,6 +373,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
372373

373374
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
374375
|-|-|-|-|-|-
376+
| 0152 |[Maximum Product Subarray](src/main/scala/g0101_0200/s0152_maximum_product_subarray/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 472 | 100.00
375377

376378
#### Day 7
377379

@@ -388,6 +390,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
388390

389391
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
390392
|-|-|-|-|-|-
393+
| 0139 |[Word Break](src/main/scala/g0101_0200/s0139_word_break/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 482 | 66.67
391394
| 0042 |[Trapping Rain Water](src/main/scala/g0001_0100/s0042_trapping_rain_water/Solution.scala)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 521 | 87.88
392395

393396
#### Day 10
@@ -591,6 +594,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
591594

592595
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
593596
|-|-|-|-|-|-
597+
| 0138 |[Copy List with Random Pointer](src/main/scala/g0101_0200/s0138_copy_list_with_random_pointer/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 477 | 96.00
594598

595599
#### Day 15
596600

@@ -769,6 +773,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
769773

770774
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
771775
|-|-|-|-|-|-
776+
| 0142 |[Linked List Cycle II](src/main/scala/g0101_0200/s0142_linked_list_cycle_ii/Solution.scala)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 501 | 61.54
772777

773778
#### Day 5 Greedy
774779

@@ -853,6 +858,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
853858

854859
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
855860
|-|-|-|-|-|-
861+
| 0148 |[Sort List](src/main/scala/g0101_0200/s0148_sort_list/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Linked_List, Divide_and_Conquer, Merge_Sort, Big_O_Time_O(log(N))_Space_O(log(N)) | 745 | 75.00
856862

857863
#### Day 5 Greedy
858864

@@ -900,6 +906,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
900906

901907
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
902908
|-|-|-|-|-|-
909+
| 0152 |[Maximum Product Subarray](src/main/scala/g0101_0200/s0152_maximum_product_subarray/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 472 | 100.00
903910

904911
#### Day 14 Sliding Window/Two Pointer
905912

@@ -948,6 +955,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
948955

949956
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
950957
|-|-|-|-|-|-
958+
| 0136 |[Single Number](src/main/scala/g0101_0200/s0136_single_number/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 530 | 78.57
951959

952960
#### Udemy Strings
953961

@@ -1007,8 +1015,12 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
10071015
|-|-|-|-|-|-
10081016
| 0114 |[Flatten Binary Tree to Linked List](src/main/scala/g0101_0200/s0114_flatten_binary_tree_to_linked_list/Solution.scala)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Big_O_Time_O(N)_Space_O(N) | 491 | 54.17
10091017
| 0024 |[Swap Nodes in Pairs](src/main/scala/g0001_0100/s0024_swap_nodes_in_pairs/Solution.scala)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 441 | 95.65
1018+
| 0142 |[Linked List Cycle II](src/main/scala/g0101_0200/s0142_linked_list_cycle_ii/Solution.scala)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 501 | 61.54
1019+
| 0141 |[Linked List Cycle](src/main/scala/g0101_0200/s0141_linked_list_cycle/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 503 | 84.25
10101020
| 0021 |[Merge Two Sorted Lists](src/main/scala/g0001_0100/s0021_merge_two_sorted_lists/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 480 | 89.72
1021+
| 0138 |[Copy List with Random Pointer](src/main/scala/g0101_0200/s0138_copy_list_with_random_pointer/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 477 | 96.00
10111022
| 0025 |[Reverse Nodes in k-Group](src/main/scala/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.scala)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 520 | 80.00
1023+
| 0146 |[LRU Cache](src/main/scala/g0101_0200/s0146_lru_cache/LRUCache.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 1590 | 85.19
10121024

10131025
#### Udemy Tree Stack Queue
10141026

@@ -1034,6 +1046,8 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
10341046

10351047
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10361048
|-|-|-|-|-|-
1049+
| 0139 |[Word Break](src/main/scala/g0101_0200/s0139_word_break/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 482 | 66.67
1050+
| 0152 |[Maximum Product Subarray](src/main/scala/g0101_0200/s0152_maximum_product_subarray/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 472 | 100.00
10371051
| 0070 |[Climbing Stairs](src/main/scala/g0001_0100/s0070_climbing_stairs/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 369 | 98.32
10381052
| 0064 |[Minimum Path Sum](src/main/scala/g0001_0100/s0064_minimum_path_sum/Solution.scala)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 521 | 92.86
10391053
| 0072 |[Edit Distance](src/main/scala/g0001_0100/s0072_edit_distance/Solution.scala)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 487 | 100.00
@@ -1099,6 +1113,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
10991113

11001114
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11011115
|-|-|-|-|-|-
1116+
| 0141 |[Linked List Cycle](src/main/scala/g0101_0200/s0141_linked_list_cycle/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 503 | 84.25
11021117
| 0021 |[Merge Two Sorted Lists](src/main/scala/g0001_0100/s0021_merge_two_sorted_lists/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 480 | 89.72
11031118

11041119
#### Day 8 Linked List
@@ -1148,6 +1163,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
11481163

11491164
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11501165
|-|-|-|-|-|-
1166+
| 0136 |[Single Number](src/main/scala/g0101_0200/s0136_single_number/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 530 | 78.57
11511167
| 0015 |[3Sum](src/main/scala/g0001_0100/s0015_3sum/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 721 | 95.24
11521168

11531169
#### Day 2 Array
@@ -1200,6 +1216,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
12001216
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12011217
|-|-|-|-|-|-
12021218
| 0002 |[Add Two Numbers](src/main/scala/g0001_0100/s0002_add_two_numbers/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)) | 571 | 75.12
1219+
| 0142 |[Linked List Cycle II](src/main/scala/g0101_0200/s0142_linked_list_cycle_ii/Solution.scala)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 501 | 61.54
12031220

12041221
#### Day 11 Linked List
12051222

@@ -1336,11 +1353,22 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
13361353

13371354
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13381355
|-|-|-|-|-|-
1356+
| 0136 |[Single Number](src/main/scala/g0101_0200/s0136_single_number/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 530 | 78.57
13391357

13401358
## Algorithms
13411359

13421360
| # | Title | Difficulty | Tag | Time, ms | Time, %
13431361
|------|----------------|-------------|-------------|----------|---------
1362+
| 0152 |[Maximum Product Subarray](src/main/scala/g0101_0200/s0152_maximum_product_subarray/Solution.scala)| 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) | 472 | 100.00
1363+
| 0148 |[Sort List](src/main/scala/g0101_0200/s0148_sort_list/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Linked_List, Divide_and_Conquer, Merge_Sort, Level_2_Day_4_Linked_List, Big_O_Time_O(log(N))_Space_O(log(N)) | 745 | 75.00
1364+
| 0146 |[LRU Cache](src/main/scala/g0101_0200/s0146_lru_cache/LRUCache.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Udemy_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 1590 | 85.19
1365+
| 0142 |[Linked List Cycle II](src/main/scala/g0101_0200/s0142_linked_list_cycle_ii/Solution.scala)| 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) | 501 | 61.54
1366+
| 0141 |[Linked List Cycle](src/main/scala/g0101_0200/s0141_linked_list_cycle/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_I_Day_7_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 503 | 84.25
1367+
| 0139 |[Word Break](src/main/scala/g0101_0200/s0139_word_break/Solution.scala)| 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, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 482 | 66.67
1368+
| 0138 |[Copy List with Random Pointer](src/main/scala/g0101_0200/s0138_copy_list_with_random_pointer/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Programming_Skills_II_Day_14, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(N) | 477 | 96.00
1369+
| 0136 |[Single Number](src/main/scala/g0101_0200/s0136_single_number/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Data_Structure_II_Day_1_Array, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Integers, Big_O_Time_O(N)_Space_O(1) | 530 | 78.57
1370+
| 0131 |[Palindrome Partitioning](src/main/scala/g0101_0200/s0131_palindrome_partitioning/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(N\*2^N)_Space_O(2^N\*N) | 797 | 100.00
1371+
| 0128 |[Longest Consecutive Sequence](src/main/scala/g0101_0200/s0128_longest_consecutive_sequence/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find, Big_O_Time_O(N_log_N)_Space_O(1) | 708 | 100.00
13441372
| 0124 |[Binary Tree Maximum Path Sum](src/main/scala/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.scala)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(N) | 523 | 89.47
13451373
| 0121 |[Best Time to Buy and Sell Stock](src/main/scala/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Data_Structure_I_Day_3_Array, Dynamic_Programming_I_Day_7, Level_1_Day_5_Greedy, Udemy_Arrays, Big_O_Time_O(N)_Space_O(1) | 762 | 98.45
13461374
| 0114 |[Flatten Binary Tree to Linked List](src/main/scala/g0101_0200/s0114_flatten_binary_tree_to_linked_list/Solution.scala)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(N) | 491 | 54.17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com_github_leetcode.random
2+
3+
class Node(var _value: Int, var _next: Node = null, var _random: Node = null) {
4+
var value: Int = _value
5+
var next: Node = _next
6+
var random: Node = _random
7+
8+
override def toString: String = {
9+
val result = new StringBuilder()
10+
result.append("[")
11+
result.append(value)
12+
if (random == null) {
13+
result.append(",null")
14+
} else {
15+
var randomIndex = 0
16+
var curr2 = this
17+
while (curr2.next != null && curr2 != random) {
18+
randomIndex += 1
19+
curr2 = curr2.next
20+
}
21+
result.append(",").append(randomIndex)
22+
}
23+
var curr = next
24+
while (curr != null) {
25+
result.append("],[")
26+
result.append(curr.value)
27+
if (curr.random == null) {
28+
result.append(",null")
29+
} else {
30+
var randomIndex = 0
31+
var curr2 = this
32+
while (curr2.next != null && curr2 != curr.random) {
33+
randomIndex += 1
34+
curr2 = curr2.next
35+
}
36+
result.append(",").append(randomIndex)
37+
}
38+
curr = curr.next
39+
}
40+
result.append("]")
41+
result.toString()
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0101_0200.s0128_longest_consecutive_sequence
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Union_Find
4+
// #Big_O_Time_O(N_log_N)_Space_O(1) #2023_11_04_Time_708_ms_(100.00%)_Space_71.7_MB_(94.55%)
5+
6+
import scala.util.Sorting.quickSort
7+
8+
object Solution {
9+
def longestConsecutive(nums: Array[Int]): Int = {
10+
if (nums.isEmpty) {
11+
return 0
12+
}
13+
14+
quickSort(nums)
15+
var max = Int.MinValue
16+
var thisMax = 1
17+
18+
for (i <- 0 until nums.length - 1) {
19+
if (nums(i + 1) == nums(i) + 1) {
20+
thisMax += 1
21+
} else if (nums(i + 1) != nums(i)) {
22+
max = Math.max(max, thisMax)
23+
thisMax = 1
24+
}
25+
}
26+
27+
Math.max(max, thisMax)
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
128\. Longest Consecutive Sequence
2+
3+
Medium
4+
5+
Given an unsorted array of integers `nums`, return _the length of the longest consecutive elements sequence._
6+
7+
You must write an algorithm that runs in `O(n)` time.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [100,4,200,1,3,2]
12+
13+
**Output:** 4
14+
15+
**Explanation:** The longest consecutive elements sequence is `[1, 2, 3, 4]`. Therefore its length is 4.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [0,3,7,2,5,8,4,6,0,1]
20+
21+
**Output:** 9
22+
23+
**Constraints:**
24+
25+
* <code>0 <= nums.length <= 10<sup>5</sup></code>
26+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0101_0200.s0131_palindrome_partitioning
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
4+
// #Backtracking #Big_O_Time_O(N*2^N)_Space_O(2^N*N)
5+
// #2023_11_04_Time_797_ms_(100.00%)_Space_84.5_MB_(72.73%)
6+
7+
object Solution {
8+
def partition(s: String): List[List[String]] = {
9+
val res = scala.collection.mutable.ListBuffer[List[String]]()
10+
backtracking(res, List.empty[String], s, 0)
11+
res.toList
12+
}
13+
14+
private def backtracking(res: scala.collection.mutable.ListBuffer[List[String]], currArr: List[String], s: String, start: Int): Unit = {
15+
if (start == s.length) {
16+
res += currArr.reverse
17+
}
18+
for (end <- start until s.length) {
19+
if (isPalindrome(s, start, end)) {
20+
backtracking(res, s.substring(start, end + 1) :: currArr, s, end + 1)
21+
}
22+
}
23+
}
24+
25+
private def isPalindrome(s: String, start: Int, end: Int): Boolean = {
26+
var left = start
27+
var right = end
28+
while (left < right && s(left) == s(right)) {
29+
left += 1
30+
right -= 1
31+
}
32+
left >= right
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
131\. Palindrome Partitioning
2+
3+
Medium
4+
5+
Given a string `s`, partition `s` such that every substring of the partition is a **palindrome**. Return all possible palindrome partitioning of `s`.
6+
7+
A **palindrome** string is a string that reads the same backward as forward.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "aab"
12+
13+
**Output:** [["a","a","b"],["aa","b"]]
14+
15+
**Example 2:**
16+
17+
**Input:** s = "a"
18+
19+
**Output:** [["a"]]
20+
21+
**Constraints:**
22+
23+
* `1 <= s.length <= 16`
24+
* `s` contains only lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package g0101_0200.s0136_single_number
2+
3+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Bit_Manipulation
4+
// #Data_Structure_II_Day_1_Array #Algorithm_I_Day_14_Bit_Manipulation #Udemy_Integers
5+
// #Big_O_Time_O(N)_Space_O(1) #2023_11_04_Time_530_ms_(78.57%)_Space_56.1_MB_(44.05%)
6+
7+
object Solution {
8+
def singleNumber(nums: Array[Int]): Int = {
9+
nums.foldLeft(0)(_ ^ _)
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
136\. Single Number
2+
3+
Easy
4+
5+
Given a **non-empty** array of integers `nums`, every element appears _twice_ except for one. Find that single one.
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,1]
12+
13+
**Output:** 1
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [4,1,2,1,2]
18+
19+
**Output:** 4
20+
21+
**Example 3:**
22+
23+
**Input:** nums = [1]
24+
25+
**Output:** 1
26+
27+
**Constraints:**
28+
29+
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
30+
* <code>-3 * 10<sup>4</sup> <= nums[i] <= 3 * 10<sup>4</sup></code>
31+
* Each element in the array appears twice except for one element which appears only once.

0 commit comments

Comments
 (0)