376. Wiggle Subsequence

不定期更新leetcode解题java答案。

采用pick one的方式选择题目。

题意为给定数组,获取满足要求的最长子串的长度。要求为前后两个数字差为绝对的正负关系(差为0不满足要求)。

例如,[1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) 。

7-1=6>0, 4-7=-3<0, 9-4=5>0, 2-9=-7<0, 5-2=3>0。

采用贪心算法先获取两相邻数的差,再进行判断是否满足正负关系。试想若两个连续的差均为大于0的数,例如[2,1,3,5,...],则选取的留下数组有[2,1,3,...]和[2,1,5,...]。

显然后者更佳(容易满足题意),由此可得出如下代码:

 1 public class Solution {
 2     public int wiggleMaxLength(int[] nums) {
 3         if(nums.length == 0)
 4             return 0;
 5
 6         int[] differences = new int[nums.length - 1];
 7
 8         for(int i = 0; i < differences.length; i++)
 9             differences[i] = nums[i + 1] - nums[i];
10
11         int count = 1;
12         int lastValue = 0;
13         int difference = 0;
14         for(int i = 0; i < differences.length; i++){
15             difference = differences[i];   //get difference value
16
17             if(lastValue == 0){
18                 if(difference != 0){
19                     lastValue = difference > 0 ? 1 : -1;
20                     count++;
21                 }
22             }else{
23                 if(difference * lastValue < 0){
24                     lastValue = lastValue > 0 ? -1 : 1;
25                     count++;
26                 }
27             }
28         }
29
30         return count;
31     }
32 }
时间: 2024-12-28 16:13:04

376. Wiggle Subsequence的相关文章

【Leetcode】376. Wiggle Subsequence

Description: A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fe

LeetCode 376. Wiggle Subsequence 摆动子序列

原题 A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than t

Leetcode 376. Wiggle Subsequence

本题要求在O(n)时间内求解.用delta储存相邻两个数的差,如果相邻的两个delta不同负号,那么说明子序列摇摆了一次.参看下图的nums的plot.这个例子的答案是7.平的线段部分我们支取最左边的一个点.除了最左边的边界点,我们要求delta != 0, 并且newDelta * delta <= 0.(这里不能只取<号),否则dot 5和7就会被忽略,因为他们的newDelta*delta = 0. 1 def wiggleMaxLength(self, nums): 2 "&

Wiggle Subsequence

A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two

[Swift]LeetCode376. 摆动序列 | Wiggle Subsequence

A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two

LeetCode &quot;Wiggle Subsequence&quot; !

Another interesting DP. Lesson learnt: how you define state is crucial.. 1. if DP[i] is defined as, longest wiggle(up\down) subseq AT number i, you will have O(n^2) solution class Solution { struct Rec { Rec(): mlen_dw(0), mlen_up(0){} Rec(int ldw, i

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea