-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1968
No.1968.Array With Elements Not Equal to Average of Neighbors
- Loading branch information
Showing
7 changed files
with
75 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
class Solution { | ||
public: | ||
vector<int> rearrangeArray(vector<int>& nums) { | ||
sort(nums.begin(), nums.end()); | ||
ranges::sort(nums); | ||
vector<int> ans; | ||
int n = nums.size(); | ||
int m = (n + 1) >> 1; | ||
for (int i = 0; i < m; ++i) { | ||
ans.push_back(nums[i]); | ||
if (i + m < n) ans.push_back(nums[i + m]); | ||
if (i + m < n) { | ||
ans.push_back(nums[i + m]); | ||
} | ||
} | ||
return ans; | ||
} | ||
}; | ||
}; |
7 changes: 3 additions & 4 deletions
7
solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
func rearrangeArray(nums []int) []int { | ||
func rearrangeArray(nums []int) (ans []int) { | ||
sort.Ints(nums) | ||
n := len(nums) | ||
m := (n + 1) >> 1 | ||
var ans []int | ||
for i := 0; i < m; i++ { | ||
ans = append(ans, nums[i]) | ||
if i+m < n { | ||
ans = append(ans, nums[i+m]) | ||
} | ||
} | ||
return ans | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function rearrangeArray(nums: number[]): number[] { | ||
nums.sort((a, b) => a - b); | ||
const n = nums.length; | ||
const m = (n + 1) >> 1; | ||
const ans: number[] = []; | ||
for (let i = 0; i < m; i++) { | ||
ans.push(nums[i]); | ||
if (i + m < n) { | ||
ans.push(nums[i + m]); | ||
} | ||
} | ||
return ans; | ||
} |
13 changes: 0 additions & 13 deletions
13
solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution2.go
This file was deleted.
Oops, something went wrong.