目录
1. 整数排列 🌟
2. 数组排序 🌟
3. 单词搜索 🌟🌟
🌟 每日一练刷题专栏 🌟
Golang每日一练 专栏
Python每日一练 专栏
C/C++每日一练 专栏
Java每日一练 专栏
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:
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED" 输出:true
示例 2:
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE" 输出:true
示例 3:
输入: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
🌟 每日一练刷题专栏 🌟
✨ 持续,努力奋斗做强刷题搬运工!
👍 点赞,你的认可是我坚持的动力!
🌟 收藏,你的青睐是我努力的方向!
✎ 评论,你的意见是我进步的财富!
☸ 主页:https://hannyang.blog.csdn.net/
Golang每日一练 专栏 | |
Python每日一练 专栏 | |
C/C++每日一练 专栏 | |
Java每日一练 专栏 |