知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

  • 练习题

    • 选择题 & 填空题

    • 代码题

      • Boxes
      • The longest ascending sequence
      • Right Rotation
        • Topic
        • Problem
        • Hint & Explain
        • Solution
          • My solution
          • Other solution 1
          • Other solution 2
          • Other solution 3
      • Book
      • Compact strings with AsciiCharSequence
      • Cinema
      • Calculate square
      • Count words
      • Robot control
      • Concat all strings without digits
      • Find the nearest number
      • Leaderboard
      • Split a list into sublists
      • Arithmetic average
      • Processing strings
  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • 练习题
  • 代码题
Jim
2022-05-07
目录

Right Rotation

# Topic

Iterating over arrays

# Problem

Right Rotation

A right rotation is an operation that shifts each element of an array to the right. For example, if an array is {1,2,3,4,5} and we right rotate it by 1, the new array will be {5,1,2,3,4}. If we rotate it by 2, the new array will be {4,5,1,2,3}. It goes like this: {1,2,3,4,5} -> {5,1,2,3,4} -> {4,5,1,2,3}.

Write a program that performs a right rotation on an array by a given number.

Note that If your solution gets the code quality warning "System.arraycopy is more efficient", please simply ignore it in respect of this code challenge.

Input format: The first line is an array of numbers. The second line is the number of rotations.

Output format: Resulting array

Sample Input 1:

1 2 3 4 5
1
1
2

Sample Output 1:

5 1 2 3 4
1

Sample Input 2:

1 2 3 4 5
2
1
2

Sample Output 2:

4 5 1 2 3
1

Sample Input 3:

1 2 3 4 5
8
1
2

Sample Output 3:

3 4 5 1 2
1

Sample Input 4:

11 21 1 41 51 78 90
4
1
2

Sample Output 4:

41 51 78 90 11 21 1
1

# Hint & Explain

You can create array of Strings in this way: String[] entrance = scanner.nextLine().split(" "); Spaces will be to divide each element of array.

Leetcode题解 - 旋转数组 (opens new window)

# Solution

# My solution

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // put your code here
        Scanner scanner = new Scanner(System.in);
        String[] array = scanner.nextLine().split(" ");
        int times = scanner.nextInt();
        String before;
        String after = array[0];

        for (int j = 1; j <= times; j++) {
            array[0] = array[array.length - 1];
            for (int i = 0; i < array.length; i++) {
                if (i + 1 < array.length) {
                    before = after;
                    after = array[i + 1];
                    array[i + 1] = before;
                }
            }
        }

        for (String item :
                array) {
            System.out.print(item + " ");
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# Other solution 1

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] array = scanner.nextLine().split(" ");
        int arrayShift = scanner.nextInt() % array.length;
        String[] newArray = new String[array.length];

        for (int i = 0; i < newArray.length; i++) {
            int index = (arrayShift + i) % array.length;
            newArray[index] = array[i];
        }

        for (String element : newArray) {
            System.out.print(element + " ");
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# Other solution 2

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        int step = scanner.nextInt();
        String[] row = s.split(" ");
        int len = row.length;
        int remember = step % len;

        String temp;

        for (int j = 0; j < remember; j++) {
            temp = row[len - 1];
            System.arraycopy(row, 0, row, 1, len - 2 + 1);
            row[0] = temp;
        }
        String str = String.join(" ", row);
        System.out.println(str);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# Other solution 3

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Integer> list = new ArrayList<>();

        String[] array = scanner.nextLine().split(" ");
        int value = scanner.nextInt();

        for (String string : array) {
            int num = Integer.parseInt(string);
            list.add(num);
        }

        Collections.rotate(list, value);

        for (Integer num : list) {
            System.out.print(num + " ");
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
编辑 (opens new window)
#Java#Problem
上次更新: 2022/09/25, 10:41:23
The longest ascending sequence
Book

← The longest ascending sequence Book→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式