From 783c98153f5302bb153e517af908ff67263518b1 Mon Sep 17 00:00:00 2001 From: "David M." Date: Tue, 5 Nov 2024 00:06:29 +0100 Subject: [PATCH 1/2] Create 0280-wiggle-sort.java --- java/0280-wiggle-sort.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 java/0280-wiggle-sort.java diff --git a/java/0280-wiggle-sort.java b/java/0280-wiggle-sort.java new file mode 100644 index 000000000..fe97a365b --- /dev/null +++ b/java/0280-wiggle-sort.java @@ -0,0 +1,14 @@ +class Solution { + public void wiggleSort(int[] nums) { + for(int i = 1; i < nums.length; i++){ + if((i % 2 == 0 && nums[i] > nums[i - 1]) + || (i % 2 == 1 && nums[i] < nums[i - 1])) + swap(nums, i, i - 1); + } + } + private void swap(int[] nums, int i, int j){ + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } +} From 1ff11164403199397d13d8f3c00e1c515f99c166 Mon Sep 17 00:00:00 2001 From: "David M." Date: Tue, 5 Nov 2024 08:20:17 +0100 Subject: [PATCH 2/2] Update 0280-wiggle-sort.java to fix spacing --- java/0280-wiggle-sort.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/java/0280-wiggle-sort.java b/java/0280-wiggle-sort.java index fe97a365b..d543e366e 100644 --- a/java/0280-wiggle-sort.java +++ b/java/0280-wiggle-sort.java @@ -1,12 +1,12 @@ class Solution { public void wiggleSort(int[] nums) { - for(int i = 1; i < nums.length; i++){ - if((i % 2 == 0 && nums[i] > nums[i - 1]) + for (int i = 1; i < nums.length; i++) { + if ((i % 2 == 0 && nums[i] > nums[i - 1]) || (i % 2 == 1 && nums[i] < nums[i - 1])) swap(nums, i, i - 1); } } - private void swap(int[] nums, int i, int j){ + private void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp;