当前位置:网站首页>排序子序列与倒置字符串
排序子序列与倒置字符串
2022-07-18 00:34:00 【InfoQ】
️排序子序列️
题目详情
输入的第一行为一个正整数n(1 ≤ n ≤ 10^5)
第二行包括n个整数A_i(1 ≤ A_i ≤ 10^9),表示数组A的每个数字。
输出一个整数表示牛牛可以将A最少划分为多少段排序子序列
6
1 2 3 2 2 1
2
解题思路
10 8 6 6 6 2 21 1 2 3 4 5 5 6 8nn=1arr[i]arr[i-1]difans- ,即表示后面的元素比前面的元素大,此时我们循环遍历,直到找到前面元素比后面元素大的情况或遍历完数组为止,那刚才遍历完的一段子序列就是一段非递减连续子序列,
ans加1,验证后续序列。
- ,即表示后面的元素比前面的元素小,此时我们循环遍历,直到找到前面元素比后面元素小的情况或遍历完数组为止,那刚才遍历完的一段子序列就是一段非递增连续子序列,
ans加1,验证后续序列。
- ,即表示两个元素是相等的,此时该序列后面为非递增的序列和为非递减的序列均可以,因此不用去处理,直接接着去验证后续序列即可。
1 2 1 2 1 2 1n=7
i=nnn+1i-1==n-1i==nn=1i==ni<ni==ni++i=n+1n+1ini==nans++i==nans1n=1源代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int ans = 0;
int i = 0;
for (i = 1; i < n; i++) {
int dif = arr[i] - arr[i - 1];
if (dif < 0) {
while (i < n && arr[i] - arr[i - 1] <= 0) {
i++;
}
ans++;
} else if (dif > 0) {
while (i < n && arr[i] - arr[i - 1] >= 0) {
i++;
}
ans++;
}
}
if (i == n) {
ans++;
}
System.out.println(ans);
}
}
总结
️倒置字符串️
题目详情
I like beijing.
beijing. like I
解题思路
I am JMUer
源代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String ss = sc.nextLine();
//将字符串依据空格,将字符串分开
String[] spss = ss.split(" ");
//首尾交换
int left = 0;
int rigth = spss.length - 1;
while (left < rigth) {
String tmp = spss[left];
spss[left++] = spss[rigth];
spss[rigth--] = tmp;
}
//加上原有的空格,将字符串数组转换成字符串
StringBuilder ans = new StringBuilder();
for (String x : spss) {
ans.append(x);
ans.append(" ");
}
System.out.println(ans);
}
}
总结
边栏推荐
猜你喜欢
随机推荐
【软件测试】——postman接口测试工具完整教程
Is it safe to open an account and buy funds on the Internet? Just contacted the fund and didn't know how to ask for guidance
Questions about Alibaba cloud's classic network
通过群晖套件搭建内网邮件服务
Lecture 21 of project practice: platform commodity Library
C# 实现winform软件最小化到系统托盘,开机自启动
PbootCMS search SQL注入漏洞
網絡上開戶買基金是否安全呢?剛接觸基金,不懂求指導
云原生—编排及管理
Servlet+JDBC表白墙
PAT乙级-B1005 继续(3n+1)猜想分数(25)【map解决】
I'm new here, so please take care of me. (actually, it's not new here ^ ^, hello CSDN, I'm here.)
使用flex布局实现局部滚动条
网上开户安全么?想知道我现在在南宁,到哪里开户比较好?
HCNP Routing&Switching之BFD
emoji 为什么叫 emoji
【干货】MySQL底层架构设计,你了解多少?
C # FTP dual network card problem
C语言之回调函数,qsort函数的定义及使用方法
Est - il sûr d'ouvrir un compte en ligne pour acheter des fonds? Je viens de toucher le Fonds, je ne sais pas comment demander des conseils.






![[advanced C language] - common memory functions](/img/46/962fdaa9c10a096a49bee42ebf51e1.png)


