Java每日一练(20230421)

简介: Java每日一练(20230421)

1. 整数排列


十位数的所有排列,数字0不在首位,且没有重复的数字。


出处:

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

代码:

import java.util.*;
public class Solution {
   public static void main(String[] args) {
      String str[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
      permutation(str, 0, str.length);
   }
   static void swap(String[] str, int start, int end) {
      String tmep = str[start];
      str[start] = str[end];
      str[end] = tmep;
   }
   static void permutation(String[] str, int start, int end) {
      if (start == end - 1) {
         for (int i = 0; i < end; i++) {
            System.out.print(str[i]);
         }
         System.out.println();
      } else {
         for (int i = start; i < end; i++) {
            if (i == 0 && str[0].equals("0"))
               continue;
            swap(str, start, i);
            permutation(str, start + 1, end);
            swap(str, start, i);
         }
      }
   }
}


输出:

1023456789

1023456798

1023456879

1023456897

1023456987

1023456978

1023457689

1023457698

1023457869

1023457896

1023457986

1023457968

1023458769

1023458796

1023458679

1023458697

1023458967

1023458976

1023459786

1023459768

1023459876

1023459867

1023459687

1023459678

1023465789

1023465798

1023465879

1023465897

1023465987

1023465978

1023467589

1023467598

1023467859

1023467895

1023467985

1023467958

1023468759

1023468795

1023468579

1023468597

1023468957

1023468975

1023469785

1023469758

1023469875

1023469857

1023469587

1023469578

1023476589

1023476598

1023476859

1023476895

1023476985

1023476958

...... 略 (共计3265920个十位数)



2. 数组排序


将随机生成的无序数组使用冒泡排序

出处:

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

代码:

class demo_sort {
    public static void main(String[] args) {
        int[] numbers = new int[] { 1, 5, 8, 2, 3, 9, 4 };
        for (int i = 0; i < numbers.length - 1; i++) {
            for (int j = 0; j < numbers.length - 1 - i; j++) {
                if (numbers[j] > numbers[j + 1]) {
                    int temp = numbers[j];
                    numbers[j] = numbers[j + 1];
                    numbers[j + 1] = temp;
                }
            }
        }
        System.out.println("从小到大排序后的结果是:");
        for (int i = 0; i < numbers.length; i++)
            System.out.print(numbers[i] + " ");
    }
}

输出:


3. 单词搜索


给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。


示例 1:

d41f067a92645388770d389f5e4d7269.jpeg

输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"

输出:true

示例 2:

56b7681c631f1e73b77fe9a7475033ea.jpeg

输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"

输出:true

示例 3:

58c1e99e8c54dfb7496d44f2d05c8e96.jpeg

输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"

输出:false


提示:

   m == board.length

   n = board[i].length

   1 <= m, n <= 6

   1 <= word.length <= 15

   board 和 word 仅由大小写英文字母组成


进阶:你可以使用搜索剪枝的技术来优化解决方案,使其在 board 更大的情况下可以更快解决问题?

出处:

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

代码:

import java.util.*;
class exist {
    public static class Solution {
        public boolean exist(char[][] board, String word) {
            int cl = board.length;
            int rl = board[0].length;
            boolean[][] flag = new boolean[cl][rl];
            for (int i = 0; i < cl; i++) {
                for (int j = 0; j < rl; j++) {
                    if (find(board, word, flag, i, j, 0))
                        return true;
                }
            }
            return false;
        }
        public boolean find(char[][] board, String word, boolean[][] flag, int i, int j, int index) {
            int cl = board.length;
            int rl = board[0].length;
            if (word.length() == index)
                return true;
            if (i < 0 || i >= cl || j >= rl || j < 0)
                return false;
            if (flag[i][j] || word.charAt(index) != board[i][j])
                return false;
            flag[i][j] = true;
            boolean judge = find(board, word, flag, i - 1, j, index + 1) || find(board, word, flag, i + 1, j, index + 1) || find(board, word, flag, i, j - 1, index + 1) || find(board, word, flag, i, j + 1, index + 1);
            flag[i][j] = false;
            return judge;
        }
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        char[][] board = {{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}};
        String word = "ABCCED";
        System.out.println(s.exist(board, word));
        word = "SEE";
        System.out.println(s.exist(board, word));
        word = "ABCB";
        System.out.println(s.exist(board, word));
   }
}





输出:

true

true

false


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