Skip to content

Commit 6791151

Browse files
committed
leetcode
1 parent 352adb0 commit 6791151

File tree

5 files changed

+246
-2
lines changed

5 files changed

+246
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
241241
- [**864.** Shortest Path to Get All Keys](ShortestPathToGetAllKeys/shortest_path_to_get_all_keys.dart)
242242
- [**1970.** Last Day Where You Can Still Cross](LastDayWhereYouCanStillCross/last_day_where_you_can_still_cross.dart)
243243
- [**859.** Buddy Strings](BuddyStrings/buddy_strings.dart)
244+
- [**137.** Single Number II](SingleNumberII/single_number_ii.dart)
244245

245246
## Reach me via
246247

SingleNumber/single_number.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class C {
9090
// Runtime: 601 ms, faster than 24.36% of Dart online submissions for Single Number.
9191
// Memory Usage: 147.2 MB, less than 52.56% of Dart online submissions for Single Number.
9292
int singleNumber(List<int> nums) {
93-
// whole llenght of the list
93+
// whole length of the list
9494
int length = nums.length;
9595
// if the list have only one value thn we will return that value which is
9696
// at first index
@@ -113,7 +113,7 @@ class C {
113113
class D {
114114
/*
115115
116-
ssuming our array has all pairs. i.e. we don’t have a single number. Then, following is true:
116+
sum-ing our array has all pairs. i.e. we don’t have a single number. Then, following is true:
117117
118118
2 (sum of unique numbers) = (sum of all numbers)
119119
Now, if we know one of the number is missing in pairs. Following must be true:

SingleNumberII/single_number_ii.dart

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
3+
4+
5+
-* 137. Single Number II *-
6+
7+
8+
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.
9+
10+
You must implement a solution with a linear runtime complexity and use only constant extra space.
11+
12+
13+
14+
Example 1:
15+
16+
Input: nums = [2,2,3,2]
17+
Output: 3
18+
Example 2:
19+
20+
Input: nums = [0,1,0,1,0,1,99]
21+
Output: 99
22+
23+
24+
Constraints:
25+
26+
1 <= nums.length <= 3 * 104
27+
-231 <= nums[i] <= 231 - 1
28+
Each element in nums appears exactly three times except for one element which appears once.
29+
30+
*/
31+
32+
import 'dart:collection';
33+
34+
class A {
35+
int singleNumber(List<int> nums) {
36+
HashMap<int, int> map = HashMap<int, int>();
37+
38+
for (int x in nums) {
39+
map[x] = (map[x] ?? 0) + 1;
40+
}
41+
42+
for (MapEntry entry in map.entries) {
43+
if (entry.value == 1) {
44+
return entry.key;
45+
}
46+
}
47+
48+
return -1;
49+
}
50+
}
51+
52+
class B {
53+
int singleNumber(List<int> nums) {
54+
int ans = 0;
55+
56+
for (int i = 0; i < 32; ++i) {
57+
int sum = 0;
58+
for (final int number in nums) {
59+
sum += (number >> i) & 1;
60+
}
61+
sum %= 3;
62+
ans |= (sum << i);
63+
}
64+
65+
// Handle negative numbers
66+
if ((ans & (1 << 31)) != 0) {
67+
ans = -(1 << 31) | (ans & ((1 << 31) - 1));
68+
}
69+
70+
return ans;
71+
}
72+
}
73+
74+
class C {
75+
int singleNumber(List<int> nums) {
76+
int ones = 0;
77+
int twos = 0;
78+
79+
for (int i = 0; i < nums.length; i++) {
80+
final int number = nums[i];
81+
ones ^= (number & ~twos);
82+
twos ^= (number & ~ones);
83+
}
84+
85+
return ones;
86+
}
87+
}

SingleNumberII/single_number_ii.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
func singleNumber(nums []int) int {
4+
var ans int = 0
5+
6+
for i := 0; i < 32; i++ {
7+
var sum int = 0
8+
for _, number := range nums {
9+
sum += (number >> i) & 1
10+
}
11+
sum %= 3
12+
ans |= (sum << i)
13+
}
14+
15+
// Handle negative numbers
16+
if (ans & (1 << 31)) != 0 {
17+
ans = -(1 << 31) | (ans & ((1 << 31) - 1))
18+
}
19+
20+
return ans
21+
}

SingleNumberII/single_number_ii.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# 🔥 3 Solutions 🔥 || Simple Fast and Easy || with Explanation 😈
2+
3+
## Brute Force
4+
5+
The provided code aims to find the single number in a list that occurs only once, while all other numbers occur twice.
6+
7+
### Algorithm Explanation
8+
9+
1. Initialize an empty HashMap called `map` to store the frequency count of each number.
10+
2. Iterate over each element `x` in the input list `nums`.
11+
3. Increment the frequency count of `x` in the `map` by 1. If `x` is not present in the `map`, initialize its count as 0 before incrementing.
12+
4. After counting the frequencies, iterate over the entries of the `map` using `map.entries`.
13+
5. For each entry, check if the value is equal to 1. If it is, return the corresponding key (the single number).
14+
6. If no single number is found, return -1.
15+
16+
### Space Complexity
17+
18+
The space complexity is O(n), where n is the number of elements in the input list `nums`. This is because additional space is required to store the frequencies of the numbers in a HashMap.
19+
20+
### Time Complexity
21+
22+
The time complexity is O(n), where n is the number of elements in the input list `nums`. The code iterates over the input list once to count the frequencies, and then iterates over the entries of the `map`. However, since the number of unique elements in `nums` is represented as `m`, the worst-case time complexity can be expressed as O(n + m) = O(n). In the worst case, where all elements are unique, `m` is equal to `n`.
23+
24+
```dart
25+
import 'dart:collection';
26+
27+
class Solution {
28+
int singleNumber(List<int> nums) {
29+
HashMap<int, int> map = HashMap<int, int>();
30+
31+
for (int x in nums) {
32+
map[x] = (map[x] ?? 0) + 1;
33+
}
34+
35+
for (MapEntry entry in map.entries) {
36+
if (entry.value == 1) {
37+
return entry.key;
38+
}
39+
}
40+
41+
return -1;
42+
}
43+
}
44+
```
45+
46+
## Bit- Manipulation
47+
48+
### Algorithm Explanation
49+
50+
The given code aims to find the single number in a list that occurs only once, while all other numbers occur three times. The code uses bitwise operations to solve the problem efficiently.
51+
52+
1. Initialize a variable `ans` as 0 to store the result.
53+
2. Iterate `i` from 0 to 31 (representing the 32 bits of an integer).
54+
3. Initialize a variable `sum` as 0 to keep track of the sum of the i-th bit for all numbers.
55+
4. Iterate over each number in the input list `nums`.
56+
- Right shift the number by `i` bits and perform a bitwise AND operation with 1 to get the i-th bit.
57+
- Add the i-th bit to `sum`.
58+
5. Take the modulus of `sum` with 3 to handle numbers occurring three times.
59+
6. Use the bitwise OR operation to set the i-th bit of `ans` based on `sum`.
60+
7. After the loop, check if the sign bit (bit 31) of `ans` is set (negative number check).
61+
- If the sign bit is set, convert `ans` to a negative number by performing necessary bitwise operations.
62+
8. Return the final result `ans`.
63+
64+
### Space Complexity
65+
66+
The space complexity of the code is O(1) since it uses a constant amount of additional space. The space required does not grow with the size of the input list.
67+
68+
### Time Complexity
69+
70+
The time complexity of the code is O(n), where n is the number of elements in the input list `nums`. The code consists of two nested loops: one loop iterating `i` from 0 to 31, and another loop iterating over each number in `nums`. However, since the number of bits being processed (32) and the number of elements in `nums` do not depend on each other, the time complexity remains linear. Therefore, the code performs an efficient linear scan of the input list to find the single number.
71+
72+
Overall, the code achieves a linear time complexity and constant space complexity, making it an efficient solution for finding the single number in the given scenario.
73+
74+
```dart
75+
class Solution {
76+
int singleNumber(List<int> nums) {
77+
int ans = 0;
78+
79+
for (int i = 0; i < 32; ++i) {
80+
int sum = 0;
81+
for (final int number in nums) {
82+
sum += (number >> i) & 1;
83+
}
84+
sum %= 3;
85+
ans |= (sum << i);
86+
}
87+
88+
// Handle negative numbers
89+
if ((ans & (1 << 31)) != 0) {
90+
ans = -(1 << 31) | (ans & ((1 << 31) - 1));
91+
}
92+
93+
return ans;
94+
}
95+
}
96+
```
97+
98+
## Magical XOR
99+
100+
### Algorithm Explanation
101+
102+
The given code aims to find the single number in a list that occurs only once, while all other numbers occur exactly twice. The code uses bitwise operations to efficiently solve the problem.
103+
104+
1. Initialize two variables `ones` and `twos` to 0. These variables are used to keep track of the bits that appear once and twice, respectively.
105+
2. Iterate over each number in the input list `nums`.
106+
- XOR (`^`) the current number with the bitwise complement of `twos` and store the result in `ones`. This operation will set the bits that appear once in `ones`.
107+
- XOR (`^`) the current number with the bitwise complement of `ones` and store the result in `twos`. This operation will set the bits that appear twice in `twos`.
108+
3. Repeat the loop for all numbers in `nums`, and after the loop, the variable `ones` will contain the single number that appears only once.
109+
110+
### Space Complexity
111+
112+
The space complexity of the code is O(1) since it uses a constant amount of additional space. The space required remains the same regardless of the size of the input list.
113+
114+
### Time Complexity
115+
116+
The time complexity of the code is O(n), where n is the number of elements in the input list `nums`. The code iterates over each number once, performing constant time operations (bitwise XOR and AND) for each number. The time complexity scales linearly with the size of the input list.
117+
118+
Overall, the code achieves an efficient linear time complexity and constant space complexity, making it an optimal solution for finding the single number in the given scenario. It leverages bitwise operations to efficiently keep track of the numbers that occur once and twice, providing an elegant solution with minimal space usage.
119+
120+
```dart
121+
class Solution {
122+
int singleNumber(List<int> nums) {
123+
int ones = 0;
124+
int twos = 0;
125+
126+
for (int i = 0; i < nums.length; i++) {
127+
final int number = nums[i];
128+
ones ^= (number & ~twos);
129+
twos ^= (number & ~ones);
130+
}
131+
132+
return ones;
133+
}
134+
}
135+
```

0 commit comments

Comments
 (0)