每天一题(3)-翻转数组

柔情只为你懂 2021-11-26 13:46 294阅读 0赞

题目

在这里插入图片描述

代码示例

  1. import java.util.Scanner;
  2. public class Main {
  3. private static void calc(Integer[] nums){
  4. int start = nums.length - 1;
  5. int end = nums.length - 1;
  6. for(int i = 0; i < nums.length - 1; i++){
  7. if(nums[i] > nums[i + 1]){
  8. start = i;
  9. break;
  10. }
  11. }
  12. for(int i = start + 1; i < nums.length - 1; i++){
  13. if (nums[i] < nums[i + 1]){
  14. end = i;
  15. break;
  16. }
  17. }
  18. if(start < end){
  19. // 判断调换后,能否在连接点递增
  20. if(start > 0 && nums[start - 1] > nums[end]){
  21. System.out.println("no");
  22. return;
  23. }
  24. if(end < (nums.length - 1) && nums[start] > nums[end + 1]){
  25. System.out.println("no");
  26. return;
  27. }
  28. // 判断后面是否是递增
  29. for(int i = end + 1; i < nums.length - 1; i++){
  30. if(nums[i] > nums[i + 1]){
  31. System.out.println("no");
  32. return;
  33. }
  34. }
  35. }
  36. System.out.println("yes");
  37. }
  38. public static void main(String[] args) {
  39. Scanner cin = new Scanner(System.in);
  40. while(cin.hasNextInt()){
  41. int n = cin.nextInt();
  42. Integer[] nums = new Integer[n];
  43. for(int i = 0; i < n; i++){
  44. nums[i] = cin.nextInt();
  45. }
  46. calc(nums);
  47. }
  48. }
  49. }

发表评论

表情:
评论列表 (有 0 条评论,294人围观)

还没有评论,来说两句吧...

相关阅读

    相关 每天道算法

    题目描述1 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一

    相关 Python每天 0000

    开学了,也继续写博客了,暑假期间也写了很多笔记。有空再复习下写在博客上。 很喜欢这个每天一题的~~~ 如有错误,请大牛指出,谢谢。 第一天是对图片的操作,那么就安装