Skip to content

Commit 898e468

Browse files
committed
3
1 parent ffda9a6 commit 898e468

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
261261
#### [376. Wiggle Subsequence](https://github.com/hitzzc/go-leetcode/tree/master/wiggle_subsequence)
262262
#### [377. Combination Sum IV](https://github.com/hitzzc/go-leetcode/tree/master/combination_sum_IV)
263263
#### [378. Kth Smallest Element in a Sorted Matrix](https://github.com/hitzzc/go-leetcode/tree/master/kth_smallest_element_in_a_sorted_matrix)
264+
#### [383. Ransom Note](https://github.com/hitzzc/go-leetcode/tree/master/ransom_note)
265+
#### [384. Shuffle an Array](https://github.com/hitzzc/go-leetcode/tree/master/shuffle_an_array)
266+
#### [386. Lexicographical Numbers](https://github.com/hitzzc/go-leetcode/tree/master/lexicographical_numbers)
264267

265268

266269

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
vector<int> lexicalOrder(int n) {
4+
vector<int> result;
5+
for (int i = 1; i <= n && i <= 9; ++i) {
6+
result.push_back(i);
7+
helper(i, n, result);
8+
}
9+
return result;
10+
}
11+
12+
void helper(int num, int n, vector<int>& result) {
13+
for (int i = 0; i <= 9; ++i) {
14+
int tmp = num*10 + i;
15+
if (tmp > n) break;
16+
result.push_back(tmp);
17+
helper(tmp, n, result);
18+
}
19+
return;
20+
}
21+
};

ransom_note/ransom_note.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
bool canConstruct(string ransomNote, string magazine) {
4+
vector<int> count(26, 0);
5+
for (auto& ch: magazine) {
6+
++count[ch-'a'];
7+
}
8+
for (auto& ch: ransomNote) {
9+
if (count[ch-'a'] <= 0) return false;
10+
--count[ch-'a'];
11+
}
12+
return true;
13+
}
14+
};

shuffle_an_array/shuffle_an_array.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
Solution(vector<int> nums): nums(nums), current(nums) {
4+
srand(time(NULL));
5+
}
6+
7+
/** Resets the array to its original configuration and return it. */
8+
vector<int> reset() {
9+
return current = nums;
10+
}
11+
12+
/** Returns a random shuffling of the array. */
13+
vector<int> shuffle() {
14+
int i = current.size();
15+
while (--i > 0) {
16+
int j = rand() % (i+1);
17+
swap(current[i], current[j]);
18+
}
19+
return current;
20+
}
21+
private:
22+
vector<int> nums, current;
23+
};
24+
25+
/**
26+
* Your Solution object will be instantiated and called as such:
27+
* Solution obj = new Solution(nums);
28+
* vector<int> param_1 = obj.reset();
29+
* vector<int> param_2 = obj.shuffle();
30+
*/

0 commit comments

Comments
 (0)