> 文章列表 > Java每日一练(20230418)

Java每日一练(20230418)

Java每日一练(20230418)

目录

1. N皇后 II  🌟🌟🌟

2. 字符串相乘  🌟🌟

3. 买卖股票的最佳时机  🌟

🌟 每日一练刷题专栏 🌟

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


1. N皇后 II

n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

给你一个整数 n ,返回 n 皇后问题 不同的解决方案的数量。

示例 1:

输入:n = 4
输出:2
解释:如上图所示,4 皇后问题存在两个不同的解法。

示例 2:

输入:n = 1
输出:1

提示:

  • 1 <= n <= 9
  • 皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。

以下程序实现了这一功能,请你填补空白处内容:

```Java
class Solution {
    private boolean col[];
    private boolean dia1[];
    private boolean dia2[];
    public int totalNQueens(int n) {
        col = new boolean[n];
        dia1 = new boolean[2 * n - 1];
        dia2 = new boolean[2 * n - 1];
        return putQueen(n, 0);
    }
    private int putQueen(int n, int index) {
        int res = 0;
        if (index == n) {
            return 1;
        }
        for (int i = 0; i < n; i++) {
            if (!col[i] && !dia1[i - index + n - 1] && !dia2[i + index]) {
                ________________________;
            }
        }
        return res;
    }
}
```

出处:

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

代码:

import java.util.*;
public class totalNQueens {public static class Solution {private boolean col[];private boolean dia1[];private boolean dia2[];public int totalNQueens(int n) {col = new boolean[n];dia1 = new boolean[2 * n - 1];dia2 = new boolean[2 * n - 1];return putQueen(n, 0);}private int putQueen(int n, int index) {int res = 0;if (index == n) {return 1;}for (int i = 0; i < n; i++) {if (!col[i] && !dia1[i - index + n - 1] && !dia2[i + index]) {col[i] = true;dia1[i - index + n - 1] = true;dia2[i + index] = true;res += putQueen(n, index + 1);col[i] = false;dia1[i - index + n - 1] = false;dia2[i + index] = false;}}return res;}}public static void main(String[] args) {Solution s = new Solution();System.out.println(s.totalNQueens(4));System.out.println(s.totalNQueens(1));}
}

输出:

2
1


2. 字符串相乘

给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。

示例 1:

输入: num1 = "2", num2 = "3"
输出: "6"

示例 2:

输入: num1 = "123", num2 = "456"
输出: "56088"

说明:

  1. num1 和 num2 的长度小于110。
  2. num1 和 num2 只包含数字 0-9
  3. num1 和 num2 均不以零开头,除非是数字 0 本身。
  4. 不能使用任何标准库的大数类型(比如 BigInteger)直接将输入转换为整数来处理

出处:

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

代码:

import java.util.*;
public class multiply {public static class Solution {public String multiply(String num1, String num2) {if (num1.equals("0") || num2.equals("0"))return "0";int m = num1.length();int n = num2.length();int[] intRes = new int[m + n - 1];for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {intRes[i + j] += (num1.charAt(i) - 48) * (num2.charAt(j) - 48);}}for (int i = intRes.length - 1; i > 0; i--) {if (intRes[i] >= 10) {intRes[i - 1] += intRes[i] / 10;intRes[i] %= 10;}}String res = "";for (int i = 0; i < intRes.length; i++) {res += String.valueOf(intRes[i]);}return res;}}public static void main(String[] args) {Solution s = new Solution();System.out.println(s.multiply("2", "3"));System.out.println(s.multiply("123", "456"));}
}

输出:

6
56088


3. 买卖股票的最佳时机

给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。

你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。

返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。

示例 1:

输入:[7,1,5,3,6,4]
输出:5
解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。

示例 2:

输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。

提示:

  • 1 <= prices.length <= 10^5
  • 0 <= prices[i] <= 10^4

出处:

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

代码:

import java.util.*;
public class maxProfit {public static class Solution {public int maxProfit(int[] prices) {int n = prices.length;if (n < 2) {return 0;}int maxProfit = 0;int minPrice = prices[0];for (int i = 1; i < n; i++) {if (prices[i] < minPrice) {minPrice = prices[i];} else if (prices[i] - minPrice > maxProfit) {maxProfit = prices[i] - minPrice;}}return maxProfit;}}public static void main(String[] args) {Solution s = new Solution();int[] nums = {7,1,5,3,6,4};System.out.println(s.maxProfit(nums));int[] nums2 = {7,6,4,3,1};System.out.println(s.maxProfit(nums2));}
}

输出:

5
0


🌟 每日一练刷题专栏 🌟

持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力! 

🌟 收藏,你的青睐是我努力的方向! 

评论,你的意见是我进步的财富!  

 主页:https://hannyang.blog.csdn.net/ 

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏