2019春季季节跳动招聘笔试(回忆版)第二题

题目描述

2019春季季节跳动招聘笔试(回忆版)第二题_第1张图片

 代码


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int n1 = scanner.nextInt();
        for (int t = 0; t < n1; t++) {
            String string = scanner.next();
            int n = string.length();
            char[] result = new char[n];
            result[0] = string.charAt(0);
            result[1] = string.charAt(1);

            int index = 2;
            for (int i = 2; i < n; i++) {
                char b1 = result[index - 2];
                char b2 = result[index - 1];
                char b3 = string.charAt(i);

                if (b1 != b2 || b2 != b3) {
                    result[index] = b3;
                    index++;
                }

            }

            for (int i = 3; i < index; i++) {
                char a1 = result[i - 3];
                char a2 = result[i - 2];
                char b1 = result[i - 1];
                char b2 = result[i];
                if (a1 == a2 && b1 == b2) {
                    result[i] = '#';
                }

            }

            StringBuilder s = new StringBuilder();
            for (int i = 0; i < index; i++) {
                if (result[i] != '#') {
                    s.append(result[i]);
                }
            }
            System.out.println(s);
        }

    }

 运行结果

2019春季季节跳动招聘笔试(回忆版)第二题_第2张图片

 

你可能感兴趣的:(校园招聘)