给定一个整数数组 A,返回满足下面条件的 非空、连续 子数组的数目:

子数组中,最左侧的元素不大于其他元素。

示例 1:

输入:[1,4,2,5,3]
输出:11
解释:有 11 个有效子数组,分别是:[1],[4],[2],[5],[3],[1,4],[2,5],[1,4,2],[2,5,3],[1,4,2,5],[1,4,2,5,3] 。

示例 2:

输入:[3,2,1]
输出:3
解释:有 3 个有效子数组,分别是:[3],[2],[1] 。

示例 3:

输入:[2,2,2]
输出:6
解释:有 6 个有效子数组,分别为是:[2],[2],[2],[2,2],[2,2],[2,2,2] 。

提示:

  1. 1 <= A.length <= 50000

  2. 0 <= A[i] <= 100000

class Solution {
public int validSubarrays(int[] nums) {
int count = nums.length;
for(int i = 0;i < nums.length;i++)
for(int j = i+1;j<nums.length;j++)
if(nums[j]>=nums[i])
count++;
else
break;
return count;
}
}

[leetcode](4.21)4. 有效子数组的数目的更多相关文章

  1. [Swift-2019力扣杯春季决赛]4. 有效子数组的数目

    给定一个整数数组 A,返回满足下面条件的 非空.连续 子数组的数目: 子数组中,最左侧的元素不大于其他元素. 示例 1: 输入:[1,4,2,5,3] 输出:11 解释:有 11 个有效子数组,分别是 ...

  2. [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  3. [LeetCode] Maximum Average Subarray I 子数组的最大平均值

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  4. [LeetCode] Subarray Sum Equals K 子数组和为K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  5. leetcode面试题42. 连续子数组的最大和

      总结一道leetcode上的高频题,反反复复遇到了好多次,特别适合作为一道动态规划入门题,本文将详细的从读题开始,介绍解题思路. 题目描述示例动态规划分析代码结果 题目   面试题42. 连续子数 ...

  6. 【LeetCode】1460. 通过翻转子数组使两个数组相等 Make Two Arrays Equal by Reversing Sub-arrays (Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 判断排序后是否相等 统计字符出现次数 日期 题目地址: ...

  7. [LeetCode] 581. 最短无序连续子数组 ☆

    描述 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, 6, 4, 8 ...

  8. Java实现 LeetCode 581 最短无序连续子数组(从两遍搜索找两个指针)

    581. 最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: ...

  9. [Swift]LeetCode974. 和可被 K 整除的子数组 | Subarray Sums Divisible by K

    Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...

随机推荐

  1. XSS之偷梁换柱--盲打垃圾短信平台

    https://www.t00ls.net/thread-49742-1-1.html

  2. 使用 python 实现π的计算

    1.π的神奇 π是一个无数人追随的真正的神奇数字.我不是很清楚一个永远重复的无理数的迷人之处.在我看来,我乐于计算π,也就是计算π的值.因为π是一个无理数,它是无限的.这就意味着任何对π的计算都仅仅是 ...

  3. Mesos源码分析(9): Test Framework的启动

    我们以Test Framework为例子解释Framework的启动方式. Test Framework的代码在src/examples/test_framework.cpp中的main函数 首先要指 ...

  4. 1.7 All components require plug-in?

    In Android, Activity, Service, ContentProvider, and BroadcastReceiver are called as four major compo ...

  5. Selenium自动化测试插件—Katalon的自述

    Katalon-一款好用的selenium自动化测试插件 Selenium 框架是目前使用较广泛的开源自动化框架,一款好的.基于界面的录制工具对于初学者来说可以快速入门:对于老手来说可以提高开发自动化 ...

  6. 3种方法来在Linux电脑上查找文件

    如果你不太了解Linux命令,那么在Linux系统里查找文件是比较困难的.只要使用多种不同的终端命令,可以很快地找到文件.Linux命令比其它操作系统的搜索功能更加强大,掌握这些命令就能你完全控制这些 ...

  7. HttpSession的API

    //获取Session对象request.getSession()request.getSession(boolean create)//获取SessionIdgetId()//获取当前session ...

  8. IntelliJ IDEA 使用 Git 并将 GitHub 作为远程代码仓库

    安装本地Git 官方下载地址:http://git-scm.com/downloads 不过这个地址一般下不动,我们可以选择在腾讯软件中心下载,速度很快. 腾讯软件中心的下载地址:https://pc ...

  9. [Swift]LeetCode448. 找到所有数组中消失的数字 | Find All Numbers Disappeared in an Array

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  10. [Swift]LeetCode676. 实现一个魔法字典 | Implement Magic Dictionary

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...