当前位置:网站首页>Share two recent classic OJ questions (arrange subsequences + find the longest continuous number string in the string)
Share two recent classic OJ questions (arrange subsequences + find the longest continuous number string in the string)
2022-04-22 23:40:00 【Li_ yizYa】
Catalog
Find the longest consecutive number string in the string
These two programming problems are of average difficulty , But you must read the question carefully and understand the meaning of the question , Doing the first question “ Sort subsequence ” when , I have never understood the meaning of non increasing or non decreasing sorting , I have been unable to start because of this problem , Finally, I learned that , Non recursive, i.e arr[i] <= arr[i+1] , Non incremental sort arr[i] >= arr[i+1] ; When doing the second question , I also misunderstood the meaning of the title , What I understand is that it's not just a continuous string of numbers , It has to be incremental , Maybe I thought more about programming at that time , I didn't read the questions carefully .
Sort subsequence
Topic link : Sort subsequence

Input description
The first line of input is an integer n(1 < n < 10^5), The second line includes n It's an integer A_i(1 < A_i < 10^9) Represents an array A Every number in
Output description
Output an integer indicating that Niuniu can A At least how many segments
for example
Input
6
1 2 3 2 2 1
Output
2
Their thinking
① Defining variables count To record the number of subsequences that can be divided into , Traverse the array starting with the first element , Judge in turn ;
② If arr[i] < arr[i+1], Enter the non increasing sequence to judge , Until the array traversal is completed or arr[i] > arr[i+1] when , Traversal stops ,count++;
③ If arr[i] > arr[i+1], Enter the non decreasing sequence to judge , Until the array traversal is completed or arr[i] < arr[i+1] when , Traversal stops ,count++;
④ If arr[i] == arr[i+1], No judgment , direct i++ Just traverse the next position .
A special case : If the input sequence is 5 5 5 5 5, When five identical data , At this time, only the fourth step will be carried out , Two or three steps will not enter judgment , At this point, it shows that the array space is larger than the actual stored data 1 The benefits of , At this time, the elements stored in the array are 5 5 5 5 5 0, This ensures that the array is not out of bounds , It also ensures the accuracy of the code .
Specific code implementation
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
// The array memory is larger than the actual data stored +1, In case the array goes out of bounds
int[] arr = new int[n+1];
for(int i = 0; i < n; i++) {
arr[i] = scan.nextInt();
}
int count = 0;
int i = 0;
while(i < n) {
if(arr[i] < arr[i+1]) {
while(i<n && arr[i] <= arr[i+1]) {
i++;
}
count++;
i++;
}else if(arr[i] == arr[i+1]) {
i++;
}else {
while(i<n && arr[i] >= arr[i+1]) {
i++;
}
count++;
i++;
}
}
System.out.println(count);
}
}
Find the longest consecutive number string in the string
Topic link : Find the longest consecutive number string in the string

Their thinking
Find the longest continuous number string in the string by counting
① Define three variables ,count,max,end.
count Used to record the length of each continuous digital string
max Used to record the longest continuous number string in the string
end Used to record the subscript of the last number in the longest continuous number string .
② Traversal string , When characters are encountered , take count Set as 0; When you encounter the first number ,count Start counting , Until the next character appears ; Compare count And max, If count > max, Make max = count.
for example : When the first number string is counted , here 
When the second segment of string statistics is finished

At this point, the traversal of the string has ended , according to max and end Directly output the longest string , The specific code is as follows
Specific code implementation
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
int max = 0;
int end = 0;
int count = 0;
for(int i = 0; i < str.length(); i++) {
if(str.charAt(i) >= '0' && str.charAt(i) <= '9') {
count++;
if(max < count) {
max = count;
end = i;
}
}else {
count = 0;
}
}
for(int i = end-max+1; i <= end; i++) {
System.out.print(str.charAt(i));
}
}
}
版权声明
本文为[Li_ yizYa]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222328215721.html
边栏推荐
- (JS)利用String对象的属性和方法实现注册和登录功能,要求用户名长度在3-10范围内,密码在6-20位
- Method of shrinking master-slave nodes in redis cluster cluster
- 【DVCon2020】基于Signoff Abstract Model的低功耗设计层级验证加速
- 读《Software Engineering at Google》(14)
- grid_map(6):grid-mapping-in-ROS编译运行
- Redis Cluster集群收缩主从节点的方法
- 【Pygame小游戏】Chrome上的小恐龙竟可以用代码玩儿了?它看起来很好玩儿的样子~
- DEJA_ 014 profile analysis of vu3d - cesium feature set
- "New programmer 003" was officially launched, and the cloud native and digital combat experience of 30 + companies such as Huawei and Alibaba
- VI / VIM editor basic operation
猜你喜欢
![[PCIe 6.0] new features of PCIe 6.0 - detailed explanation of dmwr (deferred memory write)](/img/4e/980e1eacee42a431823d0576f2e200.png)
[PCIe 6.0] new features of PCIe 6.0 - detailed explanation of dmwr (deferred memory write)

【LeetCode 剑指 Offer 36. 二叉搜索树与双向链表(中等)】

在windows7下安装vs2017插件 GLSL language integration v0.10.120.vsix报错:microsoft.visualc.redist.12
![[dvcon2020] automatic generation method of UVM sequence based on ral](/img/34/efcc6f7c6bbd07634c5c23c5e74572.png)
[dvcon2020] automatic generation method of UVM sequence based on ral

【PCIe 6.0】PCIe 6.0 新特性 - L0p 详解

visual studio 总是和搜狗输入法冲突
![[note] PCIe ltssm status transition](/img/01/0978c1d5aeebd9770f042a384f604c.png)
[note] PCIe ltssm status transition

【Pygame小游戏】Chrome上的小恐龙竟可以用代码玩儿了?它看起来很好玩儿的样子~

LeetCode 1446 - 1449
![[turtle confession collection]](/img/81/b4bacc23691e58e403f1330d0ca7cf.jpg)
[turtle confession collection] "the moon at the bottom of the sea is the moon in the sky, and the person in front of us is the sweetheart." More joy and peace for the rest of your life ~ (with 3 sourc
随机推荐
【毅力挑战】PCIe 每日一问一答(2022.02 归档)
On Spartacus product details page, use outlet to display user-defined data
[PCIe actual combat] SNPs PCIe enables SRIS mode
FileNotFoundError: [Errno 2] No such file or directory: 'image/1. Jpg 'problem solving
Read software engineering at Google (12)
Webrtc series - webrtc Foundation (VII) NAT, stun and turn (1)
JS has several red, white and black balls, including 25 red and white balls, 31 white and black balls and 28 red and black balls. Find the number of each of the three balls.
unbuntu18.04 安装 gamit10.71 problem solution
【年度总结】继往开来:回首不靠谱的2021,希冀靠谱的2022
华为机试题——HJ73 计算日期到天数转换
Lire "Software Engineering at Google" (11)
[experience sharing] share mangopapa's paper learning experience
(JS)利用String对象的属性和方法实现注册和登录功能,要求用户名长度在3-10范围内,密码在6-20位
VI / VIM editor basic operation
MySQL索引的语法是什么
漏洞利用与安全加固
线程池(通俗易懂)
【LeetCode 剑指 Offer 54. 二叉搜索树的第k大节点(简单)】
Cause analysis of SQL net message from client event
物联网标识感知