当前位置:网站首页>Leetcode problem brushing plan -- monotonic sequence
Leetcode problem brushing plan -- monotonic sequence
2022-04-22 16:55:00 【It's a little fish】

【 Preface 】:
Hello, everyone ! I'm a little fish , Starting today , The fish will brush the force buckle with everyone
partners , Rush together !!!
subject : Monotonous sequence
Original link : Power button

analysis :
This question only requires monotonous , So we Consider monotonic increasing and monotonic decreasing condition
Ideas :
We can go through Array traversal The way , use Intermediate variable to record whether it is monotonous , If two array elements satisfy monotonicity , Add one to this variable , So traverse to the end of the array .
If the whole array is monotonous , Then the value of its corresponding intermediate variable must also increase to a fixed value : The length of the array -1
Java Language version :
class Solution {
public boolean isMonotonic(int[] nums) {
// Initialize two intermediate variables , The two intermediate variables correspond to monotonic increasing and monotonic decreasing respectively
int flag1 = 0, flag2 = 0;
for (int i = 1; i < nums.length; ++i) {
// At present, when the last two numbers are equal, they also satisfy monotonicity
if (nums[i] >= nums[i - 1]) {
++flag1; // An intermediate variable that represents the incremental situation flag1 Add one
}
if (nums[i] <= nums[i - 1]) {
++flag2; // An intermediate variable representing a decreasing situation flag2 Add one
}
}
// The loop is executed nums.length - 1 Time , And only execute the qualified... Once at a time if sentence
// For example, if every execution is incremental if sentence , At the end of the cycle, the value of the intermediate variable is naturally equal to nums.length - 1,
// That is, the array is monotonically increasing , If it is monotonically decreasing, the same is true
if (flag1 == nums.length - 1 || flag2 == nums.length - 1) {
return true; // Satisfying monotone return true
}
return false;
}
}
C Language version :
bool isMonotonic(int* nums, int numsSize){
int flag1 = 0;// Initialize intermediate variable
int flag2 = 0;
int k = numsSize - 1;
for(int i = 1; i <= k; ++i){ // Cycle only k Time , That is, to make the value of the intermediate variable equal to k; Two of the loops if Statements can only execute the same... At a time
if(nums[i] >= nums[i-1]){ // For example, if each execution if It's all monotonically increasing , At the end of the loop, the value of the intermediate variable is naturally equal to k, That is, the array is single increment , return true
++flag1;
}
if(nums[i] <= nums[i-1]){
++flag2;
}
}
if(flag1 == k || flag2 == k){
return true;
}
return false;
}
Okay , That's all for today's topic , I'll see you on the next topic

版权声明
本文为[It's a little fish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221651454122.html
边栏推荐
猜你喜欢

Shiro缓存管理

GlobalMapper20 如何批量的把shp转为kml并带上属性

SDN学习之Opendaylight浅析(五)

多线程使用redis进行累加结果不对解决方案

Blue Bridge Cup practice 019

Vscode plug-in package migration and specified location

Detailed data of db107-asemi rectifier bridge

【滤波与卷积(三)】

Linux Mysql 8 修改密码 Your password does not satisfy the current policy requirements

BACKBONE,NECK,HEAD
随机推荐
Merge two ordered linked lists (delete the same content)
In 2023, Wuhan University of technology energy and power (085800) took the postgraduate entrance examination and landed. Experience guidance of predecessors in preparing for the examination
NFT platform security guide
【分布式项目】认证服务中心、社交登录OAuth2.0、单点登录、分布式Session解决方案
What is the advantage of asemi low voltage drop Schottky diode over ordinary Schottky diode?
JD side: how can a child thread get the value of the parent thread ThreadLocal? I got...
CrashSight 常规功能&特色功能介绍
Random talk on homology strategy (SOP) and cross domain resource sharing (CORS)
Respectful and modest
Summary of process and principle of solution to concurrency problem (form repeated submission problem)
Redis基本数据学习记录(一)
7 capabilities of data analysis: sorting out data needs
numpy基础大全(创建、索引、常用函数)
RTP packaging and unpacking of webrtc
中金证券开户app叫什么?股票开户怎么样佣金优惠又安全?
Shiro缓存管理
Detailed chart of chatevents inspection record (x)
Remix connects the test chain and the arbor layer 2 network
leetcode:23. 合并K个升序链表
GlobalMapper20 如何批量把dwg文件转换为dxf文件,效率神器
