Skip to content

Commit 2792765

Browse files
committed
commit
1 parent 43a926f commit 2792765

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

219. Contains Duplicate II.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
bool containsNearbyDuplicate(vector<int>& nums, int k) {
4+
unordered_map<int, int> indexMap;
5+
6+
for (int i = 0; i < nums.size(); i++) {
7+
if (indexMap.find(nums[i]) != indexMap.end()) {
8+
9+
if (i - indexMap[nums[i]] <= k)
10+
return true;
11+
}
12+
indexMap[nums[i]] = i;
13+
}
14+
15+
return false;
16+
}
17+
};

228. Summary Ranges.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
vector<string> summaryRanges(vector<int>& nums) {
4+
vector<string> res;
5+
if (nums.empty()) return res;
6+
7+
int start = nums[0]; // start of current range
8+
int end = nums[0]; // end of current range
9+
10+
for (int i = 1; i < nums.size(); ++i) {
11+
if (nums[i] == end + 1) {
12+
13+
end = nums[i];
14+
} else {
15+
16+
if (start == end) {
17+
res.push_back(to_string(start));
18+
} else {
19+
res.push_back(to_string(start) + "->" + to_string(end));
20+
}
21+
22+
start = end = nums[i];
23+
}
24+
}
25+
26+
27+
if (start == end) {
28+
res.push_back(to_string(start));
29+
} else {
30+
res.push_back(to_string(start) + "->" + to_string(end));
31+
}
32+
33+
return res;
34+
}
35+
};

268. Missing Number.cpp

Whitespace-only changes.

283. Move Zeroes.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
void moveZeroes(vector<int>& nums) {
4+
int j = 0;
5+
6+
for( int i = 0 ; i < nums.size() ; i++ ){
7+
8+
if( nums[i] != 0 ){
9+
swap( nums[i] , nums[j] );
10+
j++;
11+
}
12+
}
13+
14+
15+
}
16+
};

0 commit comments

Comments
 (0)