字符串处理

286. 数组拼接

坑比用例

3
2
2,5,6,7,,,9,5,7
1,7,4,3,,4
import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
        int Len = Integer.parseInt(in.nextLine());
        int count = Integer.parseInt(in.nextLine());
        List<List<Integer>> lists = new ArrayList<>();
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < count; i++) {   //  堆每行进行
            String[] split = in.nextLine().split("[,]+");  //  处理多逗号的情况
            int tempLen = split.length; //  长度
            for (int j = 0; j < tempLen && j < Len; j++) {    //  收入结果中
                res.add(Integer.parseInt(split[j]));
            }
            ArrayList<Integer> temp = new ArrayList<>();
            for (int j = Len; j < split.length; j++) {
                temp.add(Integer.parseInt(split[j]));
            }
            if (temp.size() != 0){
                lists.add(temp);
            }
        }
        int sum = 0;
        for (List<Integer> list : lists) {
            if (list.size() != 0){
                sum++;
            }
        }
        getResult(sum, lists, res, Len);
        StringBuilder ans = new StringBuilder();
        for (Integer re : res) {
            ans.append(re + ",");
        }
        System.out.println(ans.substring(0, ans.length() - 1));
    }

    public static void getResult(int sum, List<List<Integer>> lists, List<Integer> res, int len){
        if (sum == 0){
            return;
        }
        for (int i = 0; i < lists.size(); i++) {
            List<Integer> list = lists.get(i);
            int size = list.size();
            if (size == 0){
                continue;
            }
            for (int j = 0; j < size && j < len; j++) {
                res.add(list.get(j));
            }
            if (size <= len){
                lists.set(i, new ArrayList<>());
                sum--;
            }else { //  比你要拿的长
                for (int j = 0; j < len; j++) {
                    list.remove(0);
                }
                lists.set(i, list);
            }
        }
        getResult(sum, lists, res, len);
    }
}

使用 subList 进行优化

import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
        int Len = Integer.parseInt(in.nextLine());
        int count = Integer.parseInt(in.nextLine());
        List<List<Integer>> lists = new ArrayList<>();
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < count; i++) {   //  堆每行进行
            String[] split = in.nextLine().split("[,]+");
            int tempLen = split.length; //  长度
            for (int j = 0; j < tempLen && j < Len; j++) {    //  收入结果中
                res.add(Integer.parseInt(split[j]));
            }
            ArrayList<Integer> temp = new ArrayList<>();
            for (int j = Len; j < split.length; j++) {
                temp.add(Integer.parseInt(split[j]));
            }
            if (temp.size() != 0){
                lists.add(temp);
            }
        }
        int sum = 0;
        for (List<Integer> list : lists) {
            if (list.size() != 0){
                sum++;
            }
        }
        getResult(sum, lists, res, Len);
        StringBuilder ans = new StringBuilder();
        for (Integer re : res) {
            ans.append(re + ",");
        }
        System.out.println(ans.substring(0, ans.length() - 1));
    }

   public static void getResult(int sum, List<List<Integer>> lists, List<Integer> res, int len){
        if (sum == 0){
            return;
        }
        for (int i = 0; i < lists.size(); i++) {
            List<Integer> list = lists.get(i);
            int size = list.size();
            if (size == 0){
                continue;
            }
            for (int j = 0; j < size && j < len; j++) {  //  添加操作!!!
                res.add(list.get(j));
            }
            if (size <= len){
                list.clear();
                sum--;
            }else { //  比你要拿的长
                list.subList(0, len).clear();   //  引用类型
            }
        }
        getResult(sum, lists, res, len);
    }
}

271. 矩阵元素的边界值

[[1,2],[3,4]]  # 处理成矩阵
import java.util.Scanner;
import java.util.*;
import java.util.regex.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    static int row;
    static int col;
    static int[][] arr;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        String[] split = s.substring(2, s.length() - 2).split("\\],\\[");  // 巧妙的分割
        row = split.length;
        col = split[0].split(",").length;
        arr = new int[row][col];
        int index = 0;
        String[] temp = s.replaceAll("[\\[\\]]", "").split(",");
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                arr[i][j] = Integer.parseInt(temp[index++]);
            }
        }
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < col; i++) {
            int max = Integer.MIN_VALUE;  //  要求最大值,就先定义为 MIN_VALUE
            for (int j = 0; j < row; j++) {
                max = Math.max(max, arr[j][i]);
            }
            list.add(max);
        }
        System.out.println(Collections.min(list));
    }
}

125. 最长的完全交替连续方波信号

import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine() + "0"; //  判断最后一位是否符合
        char[] chars = s.toCharArray();
        int count = 0;  //  连续 0 的个数
        ArrayList<List<Character>> list = new ArrayList<>();
        ArrayList<Character> temp = new ArrayList<>();
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            if (aChar == '0') {
                if (count == 0) {
                    count++;
                    temp.add(aChar);
                } else { //  连续 0 超过 1
                    if (temp.size() > 1) {
                        list.add(new ArrayList<>(temp));
                        temp.clear();
                        temp.add(aChar);
                    } else {
                        temp.clear();
                        temp.add(aChar);
                    }
                }
                continue;
            }
            //  当前字符为 1 的情形
            count = 0;
            temp.add(aChar);
        }
        ArrayList<List<Character>> res = new ArrayList<>();
        for (List<Character> characters : list) {
            if (isValid(characters)) {
                res.add(characters);
            }
        }
        if (res.size() == 0){
            System.out.println(-1);
            return;
        }
        List<Character> max = Collections.max(res, (o1, o2) -> o1.size() - o2.size());
        max.stream().forEach(character -> System.out.print(character));
    }
    public static boolean isValid(List<Character> list){
        int count = 0;  //  连续 1 的个数
        for (Character character : list) {
            if (character == '1'){
                count++;
                if (count > 1){
                    return false;
                }
            }else {
                count = 0;
            }
        }
        return true;
    }
}
posted @ 2023-09-05 15:02  爱新觉罗LQ  阅读(3)  评论(0编辑  收藏  举报