Java每日一练(20230423)

简介: Java每日一练(20230423)

1. 数组元素统计


定义一个长度为5的数组arr1,用于存放5个1~9的随机整数(范围包含1和9),再定义一个长度为2的数组arr2,统计arr1中的元素对2求余等于0的个数,保存到arr2[0], 统计arr1中的元素对3求余等于0的个数,保存到arr2[1],在控制台打印输出arr2的所有元素


出处:

https://edu.csdn.net/practice/26319564

代码:


import java.util.Random;
public class RandomTest {
    public static void main(String[] args) {
        int arr1[] = new int[5];
        for (int i = 0; i < arr1.length; i++) {
            arr1[i] = new Random().nextInt(9) + 1;
        }
        int i2 = 0;
        int j3 = 0;
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] % 2 == 0) {
                i2++;
            }
            if (arr1[i] % 3 == 0) {
                j3++;
            }
        }
        int arr2[] = new int[2];
        arr2[0] = i2;
        arr2[1] = j3;
        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }
    }
}

输出:


2. 杨辉三角 II


给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行。

在「杨辉三角」中,每个数是它左上方和右上方的数的和。

53cb4e7f630b8a991ebd02133e79a9c8.gif



示例 1:

输入: rowIndex = 3

输出: [1,3,3,1]


示例 2:

输入: rowIndex = 0

输出: [1]


示例 3:

输入: rowIndex = 1

输出: [1,1]


提示:

   0 <= rowIndex <= 33

进阶:

你可以优化你的算法到 O(rowIndex) 空间复杂度吗?

出处:

https://edu.csdn.net/practice/26319565

代码:

import java.util.*;
public class getRow {
    public static class Solution {
        public List<Integer> getRow(int rowIndex) {
            List<Integer> row = new ArrayList<Integer>();
            for (int i = 0; i < rowIndex + 1; i++) {
                row.add(0, 1);
                for (int j = 1; j < row.size() - 1; j++)
                    row.set(j, row.get(j) + row.get(j + 1));
            }
            return row;
        }
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        for (int i = 0; i < 5; i++) {
            System.out.println(s.getRow(i));
        }
    }
}


输出:

[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
```Java
class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> row = new ArrayList<Integer>();
        for (int i = 0; i < rowIndex + 1; i++) {
            row.add(0, 1);
            for (int j = 1; j < row.size() - 1; j++)
                row.set(j, row.get(j) + row.get(j + 1));
        }
        return row;
    }
}
```




3. 二进制求和


给你两个二进制字符串,返回它们的和(用二进制表示)。

输入为 非空 字符串且只包含数字 10


示例 1:

输入: a = "11", b = "1"

输出: "100"


示例 2:

输入: a = "1010", b = "1011"

输出: "10101"


提示:

   每个字符串仅由字符 '0' 或 '1' 组成。

   1 <= a.length, b.length <= 10^4

   字符串如果不是 "0" ,就都不含前导零。

出处:

https://edu.csdn.net/practice/26319566

代码:


import java.util.*;
public class addBinary {
    public static class Solution {
        public String addBinary(String a, String b) {
            StringBuffer s1 = new StringBuffer(a);
            s1.reverse();
            StringBuffer s2 = new StringBuffer(b);
            s2.reverse();
            if (s1.length() > s2.length()) {
                int n = s1.length() - s2.length();
                for (int i = 0; i < n; i++) {
                    s2.append('0');
                }
            } else if (s1.length() < s2.length()) {
                int n = s2.length() - s1.length();
                for (int i = 0; i < n; i++) {
                    s1.append('0');
                }
            }
            StringBuffer stringBuffer = new StringBuffer("");
            int i = 0;
            char flag = '0';
            while (i < s1.length() && i < s2.length()) {
                if (flag == '0') {
                    if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '1') {
                        flag = '1';
                        stringBuffer.append('0');
                    } else if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '0') {
                        stringBuffer.append('0');
                    } else {
                        stringBuffer.append('1');
                    }
                } else {
                    if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '1') {
                        flag = '1';
                        stringBuffer.append('1');
                    } else if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '0') {
                        flag = '0';
                        stringBuffer.append('1');
                    } else {
                        flag = '1';
                        stringBuffer.append('0');
                    }
                }
                i++;
            }
            if (flag == '1') {
                stringBuffer.append(flag);
            }
            stringBuffer.reverse();
            return stringBuffer.toString();
        }
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        String a = "11", b = "1";
        System.out.println(s.addBinary(a,b));
        a = "1010"; b = "1011";
        System.out.println(s.addBinary(a,b));
    }
}


输出:

100

10101

目录
相关文章
|
安全 Java C++
2023-3-25 java选择题每日一练
2023-3-25 java选择题每日一练
102 1
CSDN每日一练(Java)--小艺的英文名
CSDN每日一练(Java)--小艺的英文名
|
C++ Python Rust
Rust 重载运算符|复数结构的“加减乘除”四则运算
Rust 重载运算符|复数结构的“加减乘除”四则运算
244 0
Rust 重载运算符|复数结构的“加减乘除”四则运算
|
Linux
Linux 终端命令之文件浏览(1) cat
Linux 终端命令之文件浏览(1) cat
139 0
Linux 终端命令之文件浏览(1) cat
|
Go Python Rust
Rust 编程小技巧摘选(7)
Rust 编程小技巧摘选(7)
283 0
Rust 编程小技巧摘选(7)
|
Rust 索引
Rust 编程小技巧摘选(6)
Rust 编程小技巧摘选(6)
211 1
Rust 编程小技巧摘选(6)
|
Linux Windows Ubuntu
Windows 使用 Linux 子系统,轻轻松松安装多个linux
Windows 使用 Linux 子系统,轻轻松松安装多个linux
1441 0
Windows 使用 Linux 子系统,轻轻松松安装多个linux
|
C++ Rust NoSQL
Rust 数据类型 之 类C枚举 c-like enum
Rust 数据类型 之 类C枚举 c-like enum
173 0
Rust 数据类型 之 类C枚举 c-like enum
|
Java Go C++
Golang每日一练(leetDay0120) 反转字符串中的元音字母、前K个高频元素
Golang每日一练(leetDay0120) 反转字符串中的元音字母、前K个高频元素
188 0
Golang每日一练(leetDay0120) 反转字符串中的元音字母、前K个高频元素
|
Java Go C++
Golang每日一练(leetDay0116) 路径交叉、回文对
Golang每日一练(leetDay0116) 路径交叉、回文对
141 0
Golang每日一练(leetDay0116) 路径交叉、回文对