Date 2

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);
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值