[leetcode](4.21)4. 有效子数组的数目
给定一个整数数组 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 <= A.length <= 500000 <= 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. 有效子数组的数目的更多相关文章
- [Swift-2019力扣杯春季决赛]4. 有效子数组的数目
给定一个整数数组 A,返回满足下面条件的 非空.连续 子数组的数目: 子数组中,最左侧的元素不大于其他元素. 示例 1: 输入:[1,4,2,5,3] 输出:11 解释:有 11 个有效子数组,分别是 ...
- [LeetCode] Maximum Average Subarray II 子数组的最大平均值之二
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- [LeetCode] Maximum Average Subarray I 子数组的最大平均值
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- [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 ...
- leetcode面试题42. 连续子数组的最大和
总结一道leetcode上的高频题,反反复复遇到了好多次,特别适合作为一道动态规划入门题,本文将详细的从读题开始,介绍解题思路. 题目描述示例动态规划分析代码结果 题目 面试题42. 连续子数 ...
- 【LeetCode】1460. 通过翻转子数组使两个数组相等 Make Two Arrays Equal by Reversing Sub-arrays (Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 判断排序后是否相等 统计字符出现次数 日期 题目地址: ...
- [LeetCode] 581. 最短无序连续子数组 ☆
描述 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, 6, 4, 8 ...
- Java实现 LeetCode 581 最短无序连续子数组(从两遍搜索找两个指针)
581. 最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: ...
- [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 ...
随机推荐
- 实验五:任意输入10个int类型数据,排序输出,并找出素数
源代码: package 数组;import java.util.*;public class vvv { public static void main(String[] args) { Scann ...
- [tkinter]为列表框添加滚动条
为了给列表框配备滚动条,看来很多别人的博客 终于解决了问题 ,现在我总结一下 from tkinter import * root = Tk() lb = Listbox(root) scr = Sc ...
- [Swift]LeetCode135. 分发糖果 | Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- [Swift]LeetCode228. 汇总区间 | Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- [Swift]LeetCode416. 分割等和子集 | Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [Swift]LeetCode960. 删列造序 III | Delete Columns to Make Sorted III
We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose an ...
- [Swift]LeetCode969.煎饼排序 | Pancake Sorting
Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...
- scala判断数据类型
scala判断一个数据或者对象的类型只需要在该对象后面添加 .getClass.getSimpleName : scala> 222.getClass.getSimpleName res1: S ...
- Python Matplotlib.pyplot plt 中文显示
话不多说,上代码 # -*- coding: UTF-8 -*- import matplotlib.pyplot as plt from matplotlib.font_manager import ...
- 如何让div内的多行文本上下左右居中
1.首先,如果div中的文本特别少,不超过div宽度,那么这种就非常简单了,直接line-height等于height就可以了 <style type="text/css"& ...