We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2c9a125 commit 1ef9dceCopy full SHA for 1ef9dce
scripts/algorithms/S/Sort Array By Parity/Sort Array By Parity.cpp
@@ -1,30 +1,20 @@
1
+// Runtime: 7 ms (Top 67.86%) | Memory: 16.80 MB (Top 16.67%)
2
+
3
class Solution {
4
public:
- vector<int> sortArrayByParity(vector<int>& nums)
- {
5
- vector<int> v;
6
- vector<int> odd;
7
- if(nums.size()==1)
8
9
- return nums;
10
- }
11
- for(auto it : nums)
12
13
- if(it%2==0)
14
15
- v.push_back(it);
16
17
- else
18
19
- odd.push_back(it);
+ std::vector<int> sortArrayByParity(std::vector<int>& nums) {
+ std::vector<int> result;
+ for (int i : nums) {
+ if (i % 2 == 0) {
+ result.push_back(i);
20
}
21
-
22
23
24
- for(auto i : odd)
25
26
- v.push_back(i);
+ for (int j : nums) {
+ if (j % 2 != 0) {
+ result.push_back(j);
+ }
27
28
- return v;
+ return result;
29
30
-};
+};
0 commit comments