Blage's Coding Blage's Coding
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
  • 数组

  • 链表

  • 字符串

  • 二叉树

  • 动态规划

  • 深搜回溯

  • 数学贪心

    • LCP 33. 蓄水
    • 6455. 使所有字符相等的最小成本
    • 55. 跳跃游戏
    • 2517. 礼盒的最大甜蜜度
    • 2611. 老鼠和奶酪
    • 6449. 收集巧克力
    • 1401. 圆和矩形是否有重叠
    • 2178. 拆分成最多数目的正偶数之和
    • 376. 摆动序列
      • 1.贪心
    • 649. Dota2 参议院
    • 630. 课程表 III
    • 2136. 全部开花的最早一天
    • 2731. 移动机器人
  • 堆栈队列

  • 前缀和

  • 算法设计

  • 位运算

  • WA

  • 算法
  • 数学贪心
phan
2023-07-18
目录

376. 摆动序列

# 376. 摆动序列 (opens new window)

# 1.贪心

每次将波峰和波谷加入作为子序列。

class Solution {
    public int wiggleMaxLength(int[] nums) {
        int res=1;
        int inx=1;
        while(inx<nums.length&&nums[inx]==nums[inx-1])inx++;
        if(inx==nums.length) return 1;
        boolean up=nums[inx]-nums[inx-1]>0;
        while(inx<nums.length){
            if(up){
                while(inx<nums.length&&(nums[inx-1]<=nums[inx])){
                    inx++;
                }
                res++;
                up=!up;
            }
            else{
                while(inx<nums.length&&nums[inx-1]>=nums[inx]){
                    inx++;
                }
                res++;
                up=!up;
            }
        }
        return res;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
编辑 (opens new window)
#Leetcode
上次更新: 2023/12/15, 15:49:57
2178. 拆分成最多数目的正偶数之和
649. Dota2 参议院

← 2178. 拆分成最多数目的正偶数之和 649. Dota2 参议院→

Theme by Vdoing | Copyright © 2023-2024 blageCoder
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式