0
点赞
收藏
分享

微信扫一扫

2022年2月2日刷2道简单题目过初二:一道是Leetcode-2022,另一道是Leetcod2000

你带来了我的快乐 2022-02-02 阅读 26

题目描述

2022. 将一维数组转变成二维数组

解题代码


public class Solution2022 {
    public int[][] construct2DArray(int[] original, int m, int n) {
        if (original.length != m * n) {
            return new int[0][0];
        }
        int[][] res = new int[m][n];
        int index = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                res[i][j] = original[index];
                index++;
            }
        }
        return res;
    }
}

解题结果

题目描述



2000. 反转单词前缀

这道题目,是2022年2月2日的每日一题

 解题代码

public class Solution2000 {

    public String reversePrefix(String word, char ch) {
        int index = word.indexOf(ch);
        if (index == -1) {
            return word;
        }
        StringBuilder rev = new StringBuilder(word.substring(0, index + 1));
        return rev.reverse().toString() + word.substring(index + 1);
    }
}

解题结果

 

 

举报

相关推荐

0 条评论