376:摆动序列
This commit is contained in:
parent
07a32ff2ac
commit
6aee12ffeb
95
src/main/java/leetcode/editor/cn/WiggleSubsequence.java
Normal file
95
src/main/java/leetcode/editor/cn/WiggleSubsequence.java
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
//如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为 摆动序列 。第一个差(如果存在的话)可能是正数或负数。仅有一个元素或者含两个不等元素的序列也
|
||||||
|
//视作摆动序列。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 例如, [1, 7, 4, 9, 2, 5] 是一个 摆动序列 ,因为差值 (6, -3, 5, -7, 3) 是正负交替出现的。
|
||||||
|
//
|
||||||
|
// 相反,[1, 4, 7, 2, 5] 和 [1, 7, 4, 5, 5] 不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列是因为它的最后一
|
||||||
|
//个差值为零。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 子序列 可以通过从原始序列中删除一些(也可以不删除)元素来获得,剩下的元素保持其原始顺序。
|
||||||
|
//
|
||||||
|
// 给你一个整数数组 nums ,返回 nums 中作为 摆动序列 的 最长子序列的长度 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [1,7,4,9,2,5]
|
||||||
|
//输出:6
|
||||||
|
//解释:整个序列均为摆动序列,各元素之间的差值为 (6, -3, 5, -7, 3) 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [1,17,5,10,13,15,10,5,16,8]
|
||||||
|
//输出:7
|
||||||
|
//解释:这个序列包含几个长度为 7 摆动序列。
|
||||||
|
//其中一个是 [1, 17, 10, 13, 10, 16, 8] ,各元素之间的差值为 (16, -7, 3, -3, 6, -8) 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [1,2,3,4,5,6,7,8,9]
|
||||||
|
//输出:2
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= nums.length <= 1000
|
||||||
|
// 0 <= nums[i] <= 1000
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 进阶:你能否用 O(n) 时间复杂度完成此题?
|
||||||
|
// Related Topics 贪心 数组 动态规划 👍 502 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//376:摆动序列
|
||||||
|
class WiggleSubsequence {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new WiggleSubsequence().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int wiggleMaxLength(int[] nums) {
|
||||||
|
if (nums.length < 1) {
|
||||||
|
return nums.length;
|
||||||
|
} else if (nums.length == 2) {
|
||||||
|
return nums[0] == nums[1] ? 1 : 2;
|
||||||
|
}
|
||||||
|
int dValue, ans = 0;
|
||||||
|
for (int i = 1; i < nums.length; ) {
|
||||||
|
dValue = nums[i] - nums[i - 1];
|
||||||
|
i++;
|
||||||
|
if (dValue > 0) {
|
||||||
|
ans++;
|
||||||
|
while (i < nums.length && nums[i - 1] <= nums[i]) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
} else if (dValue < 0) {
|
||||||
|
ans++;
|
||||||
|
while (i < nums.length && nums[i - 1] >= nums[i]) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ans + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
<p>如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为<strong> 摆动序列 。</strong>第一个差(如果存在的话)可能是正数或负数。仅有一个元素或者含两个不等元素的序列也视作摆动序列。</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<p>例如, <code>[1, 7, 4, 9, 2, 5]</code> 是一个 <strong>摆动序列</strong> ,因为差值 <code>(6, -3, 5, -7, 3)</code> 是正负交替出现的。</p>
|
||||||
|
</li>
|
||||||
|
<li>相反,<code>[1, 4, 7, 2, 5]</code> 和 <code>[1, 7, 4, 5, 5]</code> 不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列是因为它的最后一个差值为零。</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p><strong>子序列</strong> 可以通过从原始序列中删除一些(也可以不删除)元素来获得,剩下的元素保持其原始顺序。</p>
|
||||||
|
|
||||||
|
<p>给你一个整数数组 <code>nums</code> ,返回 <code>nums</code> 中作为 <strong>摆动序列 </strong>的 <strong>最长子序列的长度</strong> 。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [1,7,4,9,2,5]
|
||||||
|
<strong>输出:</strong>6
|
||||||
|
<strong>解释:</strong>整个序列均为摆动序列,各元素之间的差值为 (6, -3, 5, -7, 3) 。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [1,17,5,10,13,15,10,5,16,8]
|
||||||
|
<strong>输出:</strong>7
|
||||||
|
<strong>解释:</strong>这个序列包含几个长度为 7 摆动序列。
|
||||||
|
其中一个是 [1, 17, 10, 13, 10, 16, 8] ,各元素之间的差值为 (16, -7, 3, -3, 6, -8) 。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [1,2,3,4,5,6,7,8,9]
|
||||||
|
<strong>输出:</strong>2
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= nums.length <= 1000</code></li>
|
||||||
|
<li><code>0 <= nums[i] <= 1000</code></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>进阶:</strong>你能否用 <code>O(n)</code><em> </em>时间复杂度完成此题?</p>
|
||||||
|
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>动态规划</li></div></div><br><div><li>👍 502</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user