classSolution { public: intwiggleMaxLength(vector<int>& nums){ int n = nums.size(); if (n < 2) { return n; } int up = 1, down = 1; for (int i = 1; i < n; i++) { if (nums[i] > nums[i - 1]) { up = max(up, down + 1); } elseif (nums[i] < nums[i - 1]) { down = max(up + 1, down); } } returnmax(up, down); } };
[sol21-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { publicintwiggleMaxLength(int[] nums) { intn= nums.length; if (n < 2) { return n; } intup=1, down = 1; for (inti=1; i < n; i++) { if (nums[i] > nums[i - 1]) { up = Math.max(up, down + 1); } elseif (nums[i] < nums[i - 1]) { down = Math.max(up + 1, down); } } return Math.max(up, down); } }
[sol21-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution: defwiggleMaxLength(self, nums: List[int]) -> int: n = len(nums) if n < 2: return n up = down = 1 for i inrange(1, n): if nums[i] > nums[i - 1]: up = max(up, down + 1) elif nums[i] < nums[i - 1]: down = max(up + 1, down) returnmax(up, down)
funcwiggleMaxLength(nums []int)int { n := len(nums) if n < 2 { return n } up, down := 1, 1 for i := 1; i < n; i++ { if nums[i] > nums[i-1] { up = max(up, down+1) } elseif nums[i] < nums[i-1] { down = max(up+1, down) } } return max(up, down) }
funcmax(a, b int)int { if a > b { return a } return b }
[sol21-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
intwiggleMaxLength(int* nums, int numsSize) { if (numsSize < 2) { return numsSize; } int up = 1, down = 1; for (int i = 1; i < numsSize; i++) { if (nums[i] > nums[i - 1]) { up = fmax(up, down + 1); } elseif (nums[i] < nums[i - 1]) { down = fmax(up + 1, down); } } return fmax(up, down); }
[sol21-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var wiggleMaxLength = function(nums) { const n = nums.length; if (n < 2) { return n; }
let up = down = 1; for (let i = 1; i < n; i++) { if (nums[i] > nums[i - 1]) { up = Math.max(up, down + 1); } elseif (nums[i] < nums[i - 1]) { down = Math.max(up + 1, down); } } returnMath.max(up, down); };
classSolution { public: intwiggleMaxLength(vector<int>& nums){ int n = nums.size(); if (n < 2) { return n; } int up = 1, down = 1; for (int i = 1; i < n; i++) { if (nums[i] > nums[i - 1]) { up = down + 1; } elseif (nums[i] < nums[i - 1]) { down = up + 1; } } returnmax(up, down); } };
[sol22-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { publicintwiggleMaxLength(int[] nums) { intn= nums.length; if (n < 2) { return n; } intup=1, down = 1; for (inti=1; i < n; i++) { if (nums[i] > nums[i - 1]) { up = down + 1; } elseif (nums[i] < nums[i - 1]) { down = up + 1; } } return Math.max(up, down); } }
[sol22-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution: defwiggleMaxLength(self, nums: List[int]) -> int: n = len(nums) if n < 2: return n up = down = 1 for i inrange(1, n): if nums[i] > nums[i - 1]: up = down + 1 elif nums[i] < nums[i - 1]: down = up + 1 returnmax(up, down)
funcwiggleMaxLength(nums []int)int { n := len(nums) if n < 2 { return n } up, down := 1, 1 for i := 1; i < n; i++ { if nums[i] > nums[i-1] { up = down + 1 } elseif nums[i] < nums[i-1] { down = up + 1 } } return max(up, down) }
funcmax(a, b int)int { if a > b { return a } return b }
[sol22-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
intwiggleMaxLength(int* nums, int numsSize) { if (numsSize < 2) { return numsSize; } int up = 1, down = 1; for (int i = 1; i < numsSize; i++) { if (nums[i] > nums[i - 1]) { up = down + 1; } elseif (nums[i] < nums[i - 1]) { down = up + 1; } } return fmax(up, down); }
[sol22-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var wiggleMaxLength = function(nums) { const n = nums.length; if (n < 2) { return n; }
let up = down = 1; for (let i = 1; i < n; i++) { if (nums[i] > nums[i - 1]) { up = down + 1; } elseif (nums[i] < nums[i - 1]) { down = up + 1; } } returnMath.max(up, down); };