Skip to content

Commit bd1677c

Browse files
committed
Array problem solutions added
1 parent dd8de6a commit bd1677c

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

561. Array Partition I.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int arrayPairSum(vector<int>& nums) {
4+
int sum = 0;
5+
sort(nums.begin(), nums.end());
6+
int siz = nums.size();
7+
for(int i=0; i<siz; i+=2){
8+
sum += nums[i];
9+
}
10+
return sum;
11+
}
12+
};

832. Flipping an Image.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
4+
for(auto &x : A){
5+
reverse(x.begin(), x.end());
6+
for(auto &y : x){
7+
y = !(y & 1);
8+
}
9+
10+
}
11+
return A;
12+
}
13+
14+
};

905. Sort Array By Parity.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
vector<int> sortArrayByParity(vector<int>& A) {
4+
vector<int> st;
5+
deque<int> dt;
6+
for(auto x : A){
7+
if(x & 1){
8+
dt.emplace_back(x);
9+
}
10+
else{
11+
dt.emplace_front(x);
12+
}
13+
}
14+
while(!dt.empty()){
15+
st.emplace_back(dt.front());
16+
dt.pop_front();
17+
}
18+
return st;
19+
}
20+
};

977. Squares of a Sorted Array.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
vector<int> sortedSquares(vector<int>& A) {
4+
vector<int> st;
5+
for(auto x : A){
6+
st.push_back(x * x);
7+
}
8+
sort(st.begin(), st.end());
9+
return st;
10+
}
11+
};

0 commit comments

Comments
 (0)