-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayAndHash.h
222 lines (190 loc) · 5.17 KB
/
arrayAndHash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// Created by Wei on 1/31/24.
//
#ifndef NEETCODE150_ARRAYANDHASH_H
#define NEETCODE150_ARRAYANDHASH_H
#include "common.h"
//217. Contains Duplicate
class Solution217 {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> s;
for (auto num: nums) {
if (s.count(num)) {
return true;
}
s.insert(num);
}
return false;
}
};
//242. Valid Anagram
class Solution242 {
public:
bool isAnagram(string s, string t) {
if (s.length() != t.length())
return false;
unordered_map<char, int> smap;
for (auto c: s) {
smap[c]++;
}
for (auto c: t) {
if (smap[c] == 0)
return false;
else {
smap[c]--;
}
}
return true;
}
};
//1. Two Sum
class Solution1 {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
unordered_map<int, int> mp; // val -> index
for (int i = 0; i < n; i++) {
int complement = target - nums[i];
if (mp.find(complement) != mp.end()) {
return {mp[complement], i};
}
mp.insert({nums[i], i});
}
return {};
}
};
//49. Group Anagrams
class Solution49 {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> mp;
for (auto str: strs) {
string tmp = str;
sort(tmp.begin(), tmp.end());
mp[tmp].push_back(str);
}
vector<vector<string>> result;
for (auto itor: mp) {
result.push_back(itor.second);
}
return result;
}
};
//347. Top K Frequent Elements
class Solution347 {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
int n = nums.size();
unordered_map<int, int> m;
for (int i = 0; i < n; i++) {
m[nums[i]]++;
}
vector<vector<int>> buckets(n + 1);
for (auto it = m.begin(); it != m.end(); it++) {
buckets[it->second].push_back(it->first);
}
vector<int> result;
for (int i = n; i >= 0; i--) {
if (result.size() >= k) {
break;
}
if (!buckets[i].empty()) {
result.insert(result.end(), buckets[i].begin(), buckets[i].end());
}
}
return result;
}
};
//238. Product of Array Except Self
class Solution238 {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n = nums.size();
vector<int> result(n, 1);
int prefix = 1;
for (int i = 0; i < n; i++) {
result[i] = prefix;
prefix = prefix * nums[i];
}
int postfix = 1;
for (int i = n - 1; i >= 0; i--) {
result[i] = result[i] * postfix;
postfix = postfix * nums[i];
}
return result;
}
};
//36. Valid Sudoku
class Solution36 {
public:
bool isValidSudoku(vector<vector<char>>& board) {
const int cnt = 9;
bool row[cnt][cnt] = {false};
bool col[cnt][cnt] = {false};
bool sub[cnt][cnt] = {false};
for(int r = 0; r < cnt; ++r){
for(int c = 0; c < cnt; ++c){
if(board[r][c] == '.')
continue; // if not number pass
int idx = board[r][c] - '0' - 1; //char to num idx
int area = (r/3) * 3 + (c/3);
//if number already exists
if(row[r][idx] || col[c][idx] || sub[area][idx]){
return false;
}
row[r][idx] = true;
col[c][idx] = true;
sub[area][idx] = true;
}
}
return true;
}
};
//271. Encode and Decode Strings
class Solution271{
// Encodes a list of strings to a single string.
string encode(vector<string>& strs) {
string result = "";
for (int i = 0; i < strs.size(); i++) {
string str = strs[i];
result += to_string(str.size()) + "#" + str;
}
return result;
}
// Decodes a single string to a list of strings.
vector<string> decode(string s) {
vector<string> result;
int i = 0;
while (i < s.size()) {
int j = i;
while (s[j] != '#') {
j++;
}
int length = stoi(s.substr(i, j - i));
string str = s.substr(j + 1, length);
result.push_back(str);
i = j + 1 + length;
}
return result;
}
};
//128. Longest Consecutive Sequence
class Solution128 {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int>s(nums.begin(), nums.end());
int longest = 0;
for(auto &n: s){
//if this is the start of the sequence
if(!s.count(n - 1)){
int length = 1;
while(s.count(n + length))
++length;
longest = max(longest, length);
}
}
return longest;
}
};
#endif //NEETCODE150_ARRAYANDHASH_H