1.链接:排序子序列_牛客笔试题_牛客网
来源:牛客网
牛牛定义排序子序列为一个数组中一段连续的子序列,并且这段子序列是非递增或者非递减排序的。牛牛有一个长度为n的整数数组A,他现在有一个任务是把数组A分为若干段排序子序列,牛牛想知道他最少可以把这个数组分为几段排序子序列.
如样例所示,牛牛可以把数组A划分为[1,2,3]和[2,2,1]两个排序子序列,至少需要划分为2个排序子序列,所以输出2输入描述:
输入的第一行为一个正整数n(1 ≤ n ≤ 10^5) 第二行包括n个整数A_i(1 ≤ A_i ≤ 10^9),表示数组A的每个数字。
输出描述:
输出一个整数表示牛牛可以将A最少划分为多少段排序子序列示例1
输入
6 1 2 3 2 2 1输出
2
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sca = new Scanner(System.in);
int n = sca.nextInt();
int[] array = new int[n+1];
for(int i = 0; i < n;i++){
array[i] = sca.nextInt();
}
int i = 0;
int count = 0;
while(i < n){
if(array[i] < array[i+1]){
while( i < n && array[i] < array[i+1]){
i++;
}
count++;
i++;
}else if(array[i] == array[i+1]){
i++;
}else{
while( i < n && array[i] > array[i+1]){
i++;
}
count++;
i++;
}
}
System.out.println(count);
}
}
2.描述
将一句话的单词进行倒置,标点不倒置。比如 I like beijing. 经过函数后变为:beijing. like I
输入描述:
每个测试输入包含1个测试用例: I like beijing. 输入用例长度不超过100
输出描述:
依次输出倒置之后的字符串,以空格分割
示例1
输入:
I like beijing.复制输出:
beijing. like I
import java.util.*;
public class Main{
public static void reverse(char[] array,int start,int end){
while(start < end){
char tmp = array[start];
array[start] = array[end];
array[end] = tmp;
start++;
end--;
}
}
public static void main(String[] args){
Scanner sca = new Scanner(System.in);
String s = sca.nextLine();
char[] array = s.toCharArray();
int len = array.length;
reverse(array,0,len-1);
int i = 0;
while(i < len){
int j = i;
while(j < len && array[j] != ' '){
j++;
}
if(j < len){
reverse(array,i,j-1);
i = j + 1;
}else{
reverse(array,i,j-1);
i = j;
}
}
String str = new String(array);
System.out.println(str);
}
}