Skip to content

Commit 24b915a

Browse files
fix name
1 parent 73a5028 commit 24b915a

File tree

10 files changed

+205
-5
lines changed

10 files changed

+205
-5
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,13 @@ LeetCode
253253
|0371|[Sum of Two Integers](https://leetcode.com/problems/russian-doll-envelopes/) | c | [c++](./src/0371-Sum-of-Two-Integers/0371.cpp) |[python](./src/0371-Sum-of-Two-Integers/0371.py)||||[rust](./src/0371-Sum-of-Two-Integers/0371.rs)|Medium|
254254
|0377|[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/) | c | [c++](./src/0377-Combination-Sum-IV/0377.cpp) |[python](./src/0377-Combination-Sum-IV/0377.py)|||||Medium|
255255
|0389|[Find the Difference](https://leetcode.com/problems/find-the-difference/) | c | [c++](./src/0389-Find-the-Difference/0389.cpp) |[python](./src/0389-Find-the-Difference/0389.py)|||||Easy|
256-
|0384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | c | [c++](./src/0384-shuffle-an-array/0384.cpp) |||||[rust](./src/0384-shuffle-an-array/0384.rs)|Medium|
256+
|0384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | c | [c++](./src/0384-Shuffle-an-Array/0384.cpp) |||||[rust](./src/0384-Shuffle-an-Array/0384.rs)|Medium|
257257
|0392|[Is Subsequence](https://leetcode.com/problems/is-subsequence/) | c | [c++](./src/0392-Is-Subsequence/0392.cpp) |[python](./src/0392-Is-Subsequence/0392.py)|||||Medium|
258258
|0401|[Binary Watch](https://leetcode.com/problems/binary-watch/) | c | [c++](./src/0401-Binary-Watch/0401.cpp) |[python](./src/0401-Binary-Watch/0401.py)|||||Easy|
259259
|0404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | c | [c++](./src/0404-Sum-of-Left-Leaves/0404.cpp) |[python](./src/0404-Sum-of-Left-Leaves/0404.py)|||||Easy|
260260
|0409|[Longest Palindrome](https://leetcode.com/problems/longest-palindrome/)|c|[c++](./src/0409-Longest-Palindrome/0409.cpp)|[python](./src/0409-Longest-Palindrome/0409.py)|[go](./src/0409-Longest-Palindrome/0409.go)|[js](./src/0409-Longest-Palindrome/0409.js)|[java](./src/0409-Longest-Palindrome/0409.java)||Easy|
261261
|0416|[Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) | c | [c++](./src/0416-Partition-Equal-Subset-Sum/0416.cpp) |[python](./src/0416-Partition-Equal-Subset-Sum/0416.py)|[go](./src/0416-Partition-Equal-Subset-Sum/0416.go)|[js](./src/0416-Partition-Equal-Subset-Sum/0416.js)|[java](./src/0416-Partition-Equal-Subset-Sum/0416.java)||Medium|
262-
|0423|[Reconstruct Original Digits from English](https://leetcode.com/problems/reconstruct-original-digits-from-english/) || [c++](./src/0423-reconstruct-original-digits-from-english/0423.cpp) |||||[rust](./src/0423-reconstruct-original-digits-from-english/0423.rs)|Medium|
262+
|0423|[Reconstruct Original Digits from English](https://leetcode.com/problems/reconstruct-original-digits-from-english/) || [c++](./src/0423-Reconstruct-Original-Digits-from-English/0423.cpp) |||||[rust](./src/0423-Reconstruct-Original-Digits-from-English/0423.rs)|Medium|
263263
|0424|[Longest Repeating Character Replacement](https://leetcode.com/problems/partition-equal-subset-sum/) | c | [c++](./src/0424-Longest-Repeating-Character-Replacement/0424.cpp) |[python](./src/0424-Longest-Repeating-Character-Replacement/0424.py)|[go](./src/0424-Longest-Repeating-Character-Replacement/0424.go)|[js](./src/0424-Longest-Repeating-Character-Replacement/0424.js)|||Medium|
264264
|0435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/) | c | [c++](./src/0435-Non-overlapping-Intervals/0435.cpp) |[python](./src/0435-Non-overlapping-Intervals/0435.py)|[go](./src/0435-Non-overlapping-Intervals/0435.go)||||Medium|
265265
|0437|[Path Sum III](https://leetcode.com/problems/path-sum-iii/) | c | [c++](./src/0437-Path-Sum-III/0437.cpp) |[python](./src/0437-Path-Sum-III/0437.py)|||||Easy|
@@ -289,10 +289,10 @@ LeetCode
289289
|0530|[Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/) | c | [c++](./src/0530-Minimum-Absolute-Difference-in-BST/0530.cpp) |[python](./src/0530-Minimum-Absolute-Difference-in-BST/0530.py)|||||Easy|
290290
|0543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/) | c | [c++](./src/0543-Diameter-of-Binary-Tree/0543.cpp) |[python](./src/0543-Diameter-of-Binary-Tree/0543.py)|||||Easy|
291291
|0547|[Friend Circles](https://leetcode.com/problems/friend-circles/) | c | [c++](./src/0547-Friend-Circles/0547.cpp) |[python](./src/0547-Friend-Circles/0547.py)|||||Medium|
292-
|0559|[Maximum Depth of N-ary Tree](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/) || [c++](./src/0559-maximum-depth-of-n-ary-tree/0559.cpp) ||||||Easy|
292+
|0559|[Maximum Depth of N-ary Tree](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/) || [c++](./src/0559-Maximum-Depth-of-N-ary-Tree/0559.cpp) ||||||Easy|
293293
|0560|[Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | c | [c++](./src/0560-Subarray-Sum-Equals-K/0560.cpp) |[python](./src/0560-Subarray-Sum-Equals-K/0560.py)|||||Medium|
294294
|0583|[Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/)|c|[c++](./src/0583-Delete-Operation-for-Two-Strings/0583.cpp)|||||[rust](./src/0583-Delete-Operation-for-Two-Strings/0583.rs)|Medium|
295-
|0594|[Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence/) ||[c++](./src/0594-longest-harmonious-subsequence/0594.cpp)|||||[rust](./src/0594-longest-harmonious-subsequence/0594.rs)|Easy|
295+
|0594|[Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence/) ||[c++](./src/0594-Longest-Harmonious-Subsequence/0594.cpp)|||||[rust](./src/0594-Longest-Harmonious-Subsequence/0594.rs)|Easy|
296296
|0637|[Average of Levels in Binary Tree](https://leetcode.com/problems/subarray-sum-equals-k/) | c | [c++](./src/0637-Average-of-Levels-in-Binary-Tree/0637.cpp) |[python](./src/0637-Average-of-Levels-in-Binary-Tree/0637.py)|||||Easy|
297297
|0638|[Shopping Offers](https://leetcode.com/problems/shopping-offers/)||[c++](./src/0638-Shopping-Offers/0638.cpp)|||||[rust](./src/0638-Shopping-Offers/0638.rs)|Medium|
298298
|0650|[2 Keys Keyboard](https://leetcode.com/problems/2-keys-keyboard/) | c | [c++](./src/0650-2-Keys-Keyboard/0650.cpp) |[python](./src/0650-2-Keys-Keyboard/0650.py)|||||Medium|
@@ -327,7 +327,7 @@ LeetCode
327327
|0847|[Shortest Path Visiting All Nodes](https://leetcode.com/problems/shortest-path-visiting-all-nodes/) | c | [c++](./src/0847-Shortest-Path-Visiting-All-Nodes/0847.cpp) |[python](./src/0847-Shortest-Path-Visiting-All-Nodes/0847.py)|[go](./src/0847-Shortest-Path-Visiting-All-Nodes/0847.go)|[js](./src/0847-Shortest-Path-Visiting-All-Nodes/0847.js)|||Hard|
328328
|0852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | c | [c++](./src/0852-Peak-Index-in-a-Mountain-Array/0852.cpp) |[python](./src/0852-Peak-Index-in-a-Mountain-Array/0852.py)|||||Easy|
329329
|0857|[Minimum Cost to Hire K Workers](https://leetcode.com/problems/minimum-cost-to-hire-k-workers/)|c|[c++](./src/0857-Minimum-Cost-to-Hire-K-Workers/0857.cpp)|[python](./src/0857-Minimum-Cost-to-Hire-K-Workers/0857.py)|[go](./src/0857-Minimum-Cost-to-Hire-K-Workers/0857.go)|[js](./src/0857-Minimum-Cost-to-Hire-K-Workers/0857.js)|[java](./src/0857-Minimum-Cost-to-Hire-K-Workers/0857.java)||Hard|
330-
|0859|[Buddy Strings](https://leetcode.com/problems/buddy-strings/) || [c++](./src/0859-buddy-strings/0859.cpp) |||||[rust](./src/0859-buddy-strings/0859.rs)|Easy|
330+
|0859|[Buddy Strings](https://leetcode.com/problems/buddy-strings/) || [c++](./src/0859-Buddy-Strings/0859.cpp) |||||[rust](./src/0859-Buddy-Strings/0859.rs)|Easy|
331331
|0872|[Leaf-Similar Trees](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | c | [c++](./src/0852-Peak-Index-in-a-Mountain-Array/0852.cpp) |[python](./src/0852-Peak-Index-in-a-Mountain-Array/0852.py)|||||Easy|
332332
|0873|[Length of Longest Fibonacci Subsequence](https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/) | c | [c++](./src/0873-Length-of-Longest-Fibonacci-Subsequence/0873.cpp) |[python](./src/0873-Length-of-Longest-Fibonacci-Subsequence/0873.py)|||||Medium|
333333
|0874|[Walking Robot Simulation](https://leetcode.com/problems/walking-robot-simulation/) | c | [c++](./src/0874-Walking-Robot-Simulation/0874.cpp) |[python](./src/0874-Walking-Robot-Simulation/0874.py)|||||Easy|

src/0384-Shuffle-an-Array/0384.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
Solution(vector<int>& nums) {
4+
this->nums = nums;
5+
this->org = nums;
6+
}
7+
8+
vector<int> reset() {
9+
nums = org;
10+
return nums;
11+
}
12+
13+
vector<int> shuffle() {
14+
for (int i = 0; i < nums.size(); i++) {
15+
int j = i + rand() % (nums.size() - i);
16+
swap(nums[i], nums[j]);
17+
}
18+
return nums;
19+
}
20+
21+
private:
22+
vector<int> nums, org;
23+
};

src/0384-Shuffle-an-Array/0384.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use rand::Rng;
2+
3+
struct Solution {
4+
nums: Vec<i32>,
5+
org: Vec<i32>,
6+
}
7+
8+
/**
9+
* `&self` means the method takes an immutable reference.
10+
* If you need a mutable reference, change it to `&mut self` instead.
11+
*/
12+
impl Solution {
13+
fn new(nums: Vec<i32>) -> Self {
14+
Self {
15+
nums: nums.clone(),
16+
org: nums.clone(),
17+
}
18+
}
19+
20+
fn reset(&self) -> Vec<i32> {
21+
self.org.clone()
22+
}
23+
24+
fn shuffle(&self) -> Vec<i32> {
25+
let mut res = self.nums.clone();
26+
for i in 0..res.len() {
27+
let j = i + rand::thread_rng().gen_range(0..res.len() - i);
28+
res.swap(i, j);
29+
}
30+
res
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
string originalDigits(string s) {
4+
int nums[10] = {};
5+
int cnt[26] = {};
6+
for (auto c : s) cnt[c - 'a']++;
7+
nums[0] = cnt['z' - 'a'];
8+
nums[2] = cnt['w' - 'a'];
9+
nums[4] = cnt['u' - 'a'];
10+
nums[6] = cnt['x' - 'a'];
11+
nums[8] = cnt['g' - 'a'];
12+
nums[1] = cnt['o' - 'a'] - nums[0] - nums[2] - nums[4];
13+
nums[3] = cnt['h' - 'a'] - nums[8];
14+
nums[5] = cnt['f' - 'a'] - nums[4];
15+
nums[7] = cnt['s' - 'a'] - nums[6];
16+
nums[9] = cnt['i' - 'a'] - nums[5] - nums[6] - nums[8];
17+
stringstream ss;
18+
for (int i = 0; i < 10; i++) {
19+
for (int j = 0; j < nums[i]; j++) {
20+
ss << i;
21+
}
22+
}
23+
return ss.str();
24+
}
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
pub fn original_digits(s: String) -> String {
3+
let mut nums = vec![0; 10];
4+
let mut cnt = vec![0; 26];
5+
for c in s.as_bytes() {
6+
cnt[(c - b'a') as usize] += 1;
7+
}
8+
nums[0] = cnt[(b'z' - b'a') as usize];
9+
nums[2] = cnt[(b'w' - b'a') as usize];
10+
nums[4] = cnt[(b'u' - b'a') as usize];
11+
nums[6] = cnt[(b'x' - b'a') as usize];
12+
nums[8] = cnt[(b'g' - b'a') as usize];
13+
nums[1] = cnt[(b'o' - b'a') as usize] - nums[0] - nums[2] - nums[4];
14+
nums[3] = cnt[(b'h' - b'a') as usize] - nums[8];
15+
nums[5] = cnt[(b'f' - b'a') as usize] - nums[4];
16+
nums[7] = cnt[(b's' - b'a') as usize] - nums[6];
17+
nums[9] = cnt[(b'i' - b'a') as usize] - nums[5] - nums[6] - nums[8];
18+
let mut res = String::new();
19+
for i in 0..10 {
20+
res.push_str(i.to_string().repeat(nums[i]).as_str());
21+
}
22+
res
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int maxDepth(Node* root) {
4+
if (root == nullptr) {
5+
return 0;
6+
}
7+
int res = 0;
8+
for (auto c : root->children) {
9+
res = max(res, maxDepth(c));
10+
}
11+
return res + 1;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int findLHS(vector<int>& nums) {
4+
unordered_map<int, int> cnt;
5+
int res = 0;
6+
for (int num : nums) cnt[num]++;
7+
for (auto& [k, v] : cnt) {
8+
if (cnt.count(k + 1)) {
9+
res = max(res, v + cnt[k + 1]);
10+
}
11+
}
12+
return res;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn find_lhs(nums: Vec<i32>) -> i32 {
5+
let mut cnt = HashMap::new();
6+
for num in nums {
7+
*cnt.entry(num).or_insert(0) += 1;
8+
}
9+
let mut res = 0;
10+
for (&k, &v) in cnt.iter() {
11+
if let Some(&c) = cnt.get(&(k + 1)) {
12+
res = res.max(v + c);
13+
}
14+
}
15+
res
16+
}
17+
}

src/0859-Buddy-Strings/0859.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
bool buddyStrings(string s, string goal) {
4+
int n = s.size(), m = goal.size();
5+
if (n != m) return false;
6+
int a[26] = {};
7+
int b[26] = {};
8+
int t = 0;
9+
for (int i = 0; i < n; i++) {
10+
a[s[i] - 'a']++, b[goal[i] - 'a']++;
11+
if (s[i] != goal[i]) t++;
12+
}
13+
bool ok = false;
14+
for (int i = 0; i < 26; i++) {
15+
if (a[i] != b[i]) return false;
16+
if (a[i] > 1) ok = true;
17+
}
18+
return t == 2 || (t == 0 && ok);
19+
}
20+
};

src/0859-Buddy-Strings/0859.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
impl Solution {
2+
pub fn buddy_strings(s: String, goal: String) -> bool {
3+
let n = s.len();
4+
if n != goal.len() {
5+
return false;
6+
}
7+
let mut t = 0;
8+
let mut ok = false;
9+
10+
let mut a = vec![0; 26];
11+
let mut b = vec![0; 26];
12+
let s = s.as_bytes();
13+
let goal = goal.as_bytes();
14+
for i in 0..n {
15+
a[(s[i] - b'a') as usize] += 1;
16+
b[(goal[i] - b'a') as usize] += 1;
17+
if s[i] != goal[i] {
18+
t += 1;
19+
}
20+
}
21+
22+
for i in 0..26 {
23+
if a[i] != b[i] {
24+
return false;
25+
}
26+
if a[i] > 1 {
27+
ok = true;
28+
}
29+
}
30+
t == 2 || (t == 0 && ok)
31+
}
32+
}

0 commit comments

Comments
 (0)