mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-14 04:30:58 +00:00
Many bug fixes and improvements (#1270)
* Add Ruby and Kotlin icons Add the avatar of @curtishd * Update README * Synchronize zh-hant and zh versions. * Translate the pythontutor blocks to traditional Chinese * Fix en/mkdocs.yml * Update the landing page of the en version. * Fix the Dockerfile * Refine the en landingpage * Fix en landing page * Reset the README.md
This commit is contained in:
@@ -10,17 +10,17 @@ package chapter_backtracking.n_queens
|
||||
fun backtrack(
|
||||
row: Int,
|
||||
n: Int,
|
||||
state: List<MutableList<String>>,
|
||||
res: MutableList<List<List<String>>?>,
|
||||
state: MutableList<MutableList<String>>,
|
||||
res: MutableList<MutableList<MutableList<String>>?>,
|
||||
cols: BooleanArray,
|
||||
diags1: BooleanArray,
|
||||
diags2: BooleanArray
|
||||
) {
|
||||
// 當放置完所有行時,記錄解
|
||||
if (row == n) {
|
||||
val copyState: MutableList<List<String>> = ArrayList()
|
||||
val copyState = mutableListOf<MutableList<String>>()
|
||||
for (sRow in state) {
|
||||
copyState.add(ArrayList(sRow))
|
||||
copyState.add(sRow.toMutableList())
|
||||
}
|
||||
res.add(copyState)
|
||||
return
|
||||
@@ -49,11 +49,11 @@ fun backtrack(
|
||||
}
|
||||
|
||||
/* 求解 n 皇后 */
|
||||
fun nQueens(n: Int): List<List<List<String>>?> {
|
||||
fun nQueens(n: Int): MutableList<MutableList<MutableList<String>>?> {
|
||||
// 初始化 n*n 大小的棋盤,其中 'Q' 代表皇后,'#' 代表空位
|
||||
val state: MutableList<MutableList<String>> = ArrayList()
|
||||
val state = mutableListOf<MutableList<String>>()
|
||||
for (i in 0..<n) {
|
||||
val row: MutableList<String> = ArrayList()
|
||||
val row = mutableListOf<String>()
|
||||
for (j in 0..<n) {
|
||||
row.add("#")
|
||||
}
|
||||
@@ -62,7 +62,7 @@ fun nQueens(n: Int): List<List<List<String>>?> {
|
||||
val cols = BooleanArray(n) // 記錄列是否有皇后
|
||||
val diags1 = BooleanArray(2 * n - 1) // 記錄主對角線上是否有皇后
|
||||
val diags2 = BooleanArray(2 * n - 1) // 記錄次對角線上是否有皇后
|
||||
val res: MutableList<List<List<String>>?> = ArrayList()
|
||||
val res = mutableListOf<MutableList<MutableList<String>>?>()
|
||||
|
||||
backtrack(0, n, state, res, cols, diags1, diags2)
|
||||
|
||||
@@ -72,7 +72,7 @@ fun nQueens(n: Int): List<List<List<String>>?> {
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
val n = 4
|
||||
val res: List<List<List<String?>?>?> = nQueens(n)
|
||||
val res = nQueens(n)
|
||||
|
||||
println("輸入棋盤長寬為 $n")
|
||||
println("皇后放置方案共有 ${res.size} 種")
|
||||
|
||||
@@ -11,11 +11,11 @@ fun backtrack(
|
||||
state: MutableList<Int>,
|
||||
choices: IntArray,
|
||||
selected: BooleanArray,
|
||||
res: MutableList<List<Int>?>
|
||||
res: MutableList<MutableList<Int>?>
|
||||
) {
|
||||
// 當狀態長度等於元素數量時,記錄解
|
||||
if (state.size == choices.size) {
|
||||
res.add(ArrayList(state))
|
||||
res.add(state.toMutableList())
|
||||
return
|
||||
}
|
||||
// 走訪所有選擇
|
||||
@@ -36,9 +36,9 @@ fun backtrack(
|
||||
}
|
||||
|
||||
/* 全排列 I */
|
||||
fun permutationsI(nums: IntArray): List<List<Int>?> {
|
||||
val res: MutableList<List<Int>?> = ArrayList()
|
||||
backtrack(ArrayList(), nums, BooleanArray(nums.size), res)
|
||||
fun permutationsI(nums: IntArray): MutableList<MutableList<Int>?> {
|
||||
val res = mutableListOf<MutableList<Int>?>()
|
||||
backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)
|
||||
return res
|
||||
}
|
||||
|
||||
|
||||
@@ -15,11 +15,11 @@ fun backtrack(
|
||||
) {
|
||||
// 當狀態長度等於元素數量時,記錄解
|
||||
if (state.size == choices.size) {
|
||||
res.add(ArrayList(state))
|
||||
res.add(state.toMutableList())
|
||||
return
|
||||
}
|
||||
// 走訪所有選擇
|
||||
val duplicated: MutableSet<Int> = HashSet()
|
||||
val duplicated = HashSet<Int>()
|
||||
for (i in choices.indices) {
|
||||
val choice = choices[i]
|
||||
// 剪枝:不允許重複選擇元素 且 不允許重複選擇相等元素
|
||||
@@ -39,15 +39,14 @@ fun backtrack(
|
||||
|
||||
/* 全排列 II */
|
||||
fun permutationsII(nums: IntArray): MutableList<MutableList<Int>?> {
|
||||
val res: MutableList<MutableList<Int>?> = ArrayList()
|
||||
backtrack(ArrayList(), nums, BooleanArray(nums.size), res)
|
||||
val res = mutableListOf<MutableList<Int>?>()
|
||||
backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)
|
||||
return res
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
val nums = intArrayOf(1, 2, 2)
|
||||
|
||||
val res = permutationsII(nums)
|
||||
|
||||
println("輸入陣列 nums = ${nums.contentToString()}")
|
||||
|
||||
@@ -16,7 +16,7 @@ fun preOrder(root: TreeNode?) {
|
||||
if (root == null) {
|
||||
return
|
||||
}
|
||||
if (root.value == 7) {
|
||||
if (root._val == 7) {
|
||||
// 記錄解
|
||||
res!!.add(root)
|
||||
}
|
||||
@@ -31,13 +31,13 @@ fun main() {
|
||||
printTree(root)
|
||||
|
||||
// 前序走訪
|
||||
res = ArrayList()
|
||||
res = mutableListOf()
|
||||
preOrder(root)
|
||||
|
||||
println("\n輸出所有值為 7 的節點")
|
||||
val vals: MutableList<Int> = ArrayList()
|
||||
for (node in res as ArrayList<TreeNode>) {
|
||||
vals.add(node.value)
|
||||
val vals = mutableListOf<Int>()
|
||||
for (node in res!!) {
|
||||
vals.add(node._val)
|
||||
}
|
||||
println(vals)
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import utils.TreeNode
|
||||
import utils.printTree
|
||||
|
||||
var path: MutableList<TreeNode>? = null
|
||||
var res: MutableList<List<TreeNode>>? = null
|
||||
var res: MutableList<MutableList<TreeNode>>? = null
|
||||
|
||||
/* 前序走訪:例題二 */
|
||||
fun preOrder(root: TreeNode?) {
|
||||
@@ -19,9 +19,9 @@ fun preOrder(root: TreeNode?) {
|
||||
}
|
||||
// 嘗試
|
||||
path!!.add(root)
|
||||
if (root.value == 7) {
|
||||
if (root._val == 7) {
|
||||
// 記錄解
|
||||
res!!.add(ArrayList(path!!))
|
||||
res!!.add(path!!.toMutableList())
|
||||
}
|
||||
preOrder(root.left)
|
||||
preOrder(root.right)
|
||||
@@ -36,16 +36,16 @@ fun main() {
|
||||
printTree(root)
|
||||
|
||||
// 前序走訪
|
||||
path = java.util.ArrayList<TreeNode>()
|
||||
res = java.util.ArrayList<List<TreeNode>>()
|
||||
path = mutableListOf()
|
||||
res = mutableListOf()
|
||||
preOrder(root)
|
||||
|
||||
println("\n輸出所有根節點到節點 7 的路徑")
|
||||
for (path in res as ArrayList<List<TreeNode>>) {
|
||||
val values: MutableList<Int> = ArrayList()
|
||||
for (path in res!!) {
|
||||
val _vals = mutableListOf<Int>()
|
||||
for (node in path) {
|
||||
values.add(node.value)
|
||||
_vals.add(node._val)
|
||||
}
|
||||
println(values)
|
||||
println(_vals)
|
||||
}
|
||||
}
|
||||
@@ -10,19 +10,19 @@ import utils.TreeNode
|
||||
import utils.printTree
|
||||
|
||||
var path: MutableList<TreeNode>? = null
|
||||
var res: MutableList<List<TreeNode>>? = null
|
||||
var res: MutableList<MutableList<TreeNode>>? = null
|
||||
|
||||
/* 前序走訪:例題三 */
|
||||
fun preOrder(root: TreeNode?) {
|
||||
// 剪枝
|
||||
if (root == null || root.value == 3) {
|
||||
if (root == null || root._val == 3) {
|
||||
return
|
||||
}
|
||||
// 嘗試
|
||||
path!!.add(root)
|
||||
if (root.value == 7) {
|
||||
if (root._val == 7) {
|
||||
// 記錄解
|
||||
res!!.add(ArrayList(path!!))
|
||||
res!!.add(path!!.toMutableList())
|
||||
}
|
||||
preOrder(root.left)
|
||||
preOrder(root.right)
|
||||
@@ -37,16 +37,16 @@ fun main() {
|
||||
printTree(root)
|
||||
|
||||
// 前序走訪
|
||||
path = ArrayList()
|
||||
res = ArrayList()
|
||||
path = mutableListOf()
|
||||
res = mutableListOf()
|
||||
preOrder(root)
|
||||
|
||||
println("\n輸出所有根節點到節點 7 的路徑,路徑中不包含值為 3 的節點")
|
||||
for (path in res as ArrayList<List<TreeNode>>) {
|
||||
val values: MutableList<Int> = ArrayList()
|
||||
for (path in res!!) {
|
||||
val _vals = mutableListOf<Int>()
|
||||
for (node in path) {
|
||||
values.add(node.value)
|
||||
_vals.add(node._val)
|
||||
}
|
||||
println(values)
|
||||
println(_vals)
|
||||
}
|
||||
}
|
||||
@@ -8,21 +8,20 @@ package chapter_backtracking.preorder_traversal_iii_template
|
||||
|
||||
import utils.TreeNode
|
||||
import utils.printTree
|
||||
import java.util.*
|
||||
|
||||
/* 判斷當前狀態是否為解 */
|
||||
fun isSolution(state: List<TreeNode?>): Boolean {
|
||||
return state.isNotEmpty() && state[state.size - 1]?.value == 7
|
||||
fun isSolution(state: MutableList<TreeNode?>): Boolean {
|
||||
return state.isNotEmpty() && state[state.size - 1]?._val == 7
|
||||
}
|
||||
|
||||
/* 記錄解 */
|
||||
fun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<List<TreeNode?>?>) {
|
||||
res.add(state?.let { ArrayList(it) })
|
||||
fun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<MutableList<TreeNode?>?>) {
|
||||
res.add(state!!.toMutableList())
|
||||
}
|
||||
|
||||
/* 判斷在當前狀態下,該選擇是否合法 */
|
||||
fun isValid(state: List<TreeNode?>?, choice: TreeNode?): Boolean {
|
||||
return choice != null && choice.value != 3
|
||||
fun isValid(state: MutableList<TreeNode?>?, choice: TreeNode?): Boolean {
|
||||
return choice != null && choice._val != 3
|
||||
}
|
||||
|
||||
/* 更新狀態 */
|
||||
@@ -38,8 +37,8 @@ fun undoChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {
|
||||
/* 回溯演算法:例題三 */
|
||||
fun backtrack(
|
||||
state: MutableList<TreeNode?>,
|
||||
choices: List<TreeNode?>,
|
||||
res: MutableList<List<TreeNode?>?>
|
||||
choices: MutableList<TreeNode?>,
|
||||
res: MutableList<MutableList<TreeNode?>?>
|
||||
) {
|
||||
// 檢查是否為解
|
||||
if (isSolution(state)) {
|
||||
@@ -53,7 +52,7 @@ fun backtrack(
|
||||
// 嘗試:做出選擇,更新狀態
|
||||
makeChoice(state, choice)
|
||||
// 進行下一輪選擇
|
||||
backtrack(state, listOf(choice!!.left, choice.right), res)
|
||||
backtrack(state, mutableListOf(choice!!.left, choice.right), res)
|
||||
// 回退:撤銷選擇,恢復到之前的狀態
|
||||
undoChoice(state, choice)
|
||||
}
|
||||
@@ -67,15 +66,15 @@ fun main() {
|
||||
printTree(root)
|
||||
|
||||
// 回溯演算法
|
||||
val res: MutableList<List<TreeNode?>?> = ArrayList()
|
||||
backtrack(ArrayList(), mutableListOf(root), res)
|
||||
val res = mutableListOf<MutableList<TreeNode?>?>()
|
||||
backtrack(mutableListOf(), mutableListOf(root), res)
|
||||
|
||||
println("\n輸出所有根節點到節點 7 的路徑,要求路徑中不包含值為 3 的節點")
|
||||
for (path in res) {
|
||||
val vals = ArrayList<Int>()
|
||||
val vals = mutableListOf<Int>()
|
||||
for (node in path!!) {
|
||||
if (node != null) {
|
||||
vals.add(node.value)
|
||||
vals.add(node._val)
|
||||
}
|
||||
}
|
||||
println(vals)
|
||||
|
||||
@@ -6,19 +6,17 @@
|
||||
|
||||
package chapter_backtracking.subset_sum_i
|
||||
|
||||
import java.util.*
|
||||
|
||||
/* 回溯演算法:子集和 I */
|
||||
fun backtrack(
|
||||
state: MutableList<Int>,
|
||||
target: Int,
|
||||
choices: IntArray,
|
||||
start: Int,
|
||||
res: MutableList<List<Int>?>
|
||||
res: MutableList<MutableList<Int>?>
|
||||
) {
|
||||
// 子集和等於 target 時,記錄解
|
||||
if (target == 0) {
|
||||
res.add(ArrayList(state))
|
||||
res.add(state.toMutableList())
|
||||
return
|
||||
}
|
||||
// 走訪所有選擇
|
||||
@@ -39,11 +37,11 @@ fun backtrack(
|
||||
}
|
||||
|
||||
/* 求解子集和 I */
|
||||
fun subsetSumI(nums: IntArray, target: Int): List<List<Int>?> {
|
||||
val state: MutableList<Int> = ArrayList() // 狀態(子集)
|
||||
Arrays.sort(nums) // 對 nums 進行排序
|
||||
fun subsetSumI(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {
|
||||
val state = mutableListOf<Int>() // 狀態(子集)
|
||||
nums.sort() // 對 nums 進行排序
|
||||
val start = 0 // 走訪起始點
|
||||
val res: MutableList<List<Int>?> = ArrayList() // 結果串列(子集串列)
|
||||
val res = mutableListOf<MutableList<Int>?>() // 結果串列(子集串列)
|
||||
backtrack(state, target, nums, start, res)
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -12,11 +12,11 @@ fun backtrack(
|
||||
target: Int,
|
||||
total: Int,
|
||||
choices: IntArray,
|
||||
res: MutableList<List<Int>?>
|
||||
res: MutableList<MutableList<Int>?>
|
||||
) {
|
||||
// 子集和等於 target 時,記錄解
|
||||
if (total == target) {
|
||||
res.add(ArrayList(state))
|
||||
res.add(state.toMutableList())
|
||||
return
|
||||
}
|
||||
// 走訪所有選擇
|
||||
@@ -35,10 +35,10 @@ fun backtrack(
|
||||
}
|
||||
|
||||
/* 求解子集和 I(包含重複子集) */
|
||||
fun subsetSumINaive(nums: IntArray, target: Int): List<List<Int>?> {
|
||||
val state: MutableList<Int> = ArrayList() // 狀態(子集)
|
||||
fun subsetSumINaive(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {
|
||||
val state = mutableListOf<Int>() // 狀態(子集)
|
||||
val total = 0 // 子集和
|
||||
val res: MutableList<List<Int>?> = ArrayList() // 結果串列(子集串列)
|
||||
val res = mutableListOf<MutableList<Int>?>() // 結果串列(子集串列)
|
||||
backtrack(state, target, total, nums, res)
|
||||
return res
|
||||
}
|
||||
@@ -47,8 +47,7 @@ fun subsetSumINaive(nums: IntArray, target: Int): List<List<Int>?> {
|
||||
fun main() {
|
||||
val nums = intArrayOf(3, 4, 5)
|
||||
val target = 9
|
||||
|
||||
val res: List<List<Int>?> = subsetSumINaive(nums, target)
|
||||
val res = subsetSumINaive(nums, target)
|
||||
|
||||
println("輸入陣列 nums = ${nums.contentToString()}, target = $target")
|
||||
println("所有和等於 $target 的子集 res = $res")
|
||||
|
||||
@@ -6,19 +6,17 @@
|
||||
|
||||
package chapter_backtracking.subset_sum_ii
|
||||
|
||||
import java.util.*
|
||||
|
||||
/* 回溯演算法:子集和 II */
|
||||
fun backtrack(
|
||||
state: MutableList<Int>,
|
||||
target: Int,
|
||||
choices: IntArray,
|
||||
start: Int,
|
||||
res: MutableList<List<Int>?>
|
||||
res: MutableList<MutableList<Int>?>
|
||||
) {
|
||||
// 子集和等於 target 時,記錄解
|
||||
if (target == 0) {
|
||||
res.add(ArrayList(state))
|
||||
res.add(state.toMutableList())
|
||||
return
|
||||
}
|
||||
// 走訪所有選擇
|
||||
@@ -44,11 +42,11 @@ fun backtrack(
|
||||
}
|
||||
|
||||
/* 求解子集和 II */
|
||||
fun subsetSumII(nums: IntArray, target: Int): List<List<Int>?> {
|
||||
val state: MutableList<Int> = ArrayList() // 狀態(子集)
|
||||
Arrays.sort(nums) // 對 nums 進行排序
|
||||
fun subsetSumII(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {
|
||||
val state = mutableListOf<Int>() // 狀態(子集)
|
||||
nums.sort() // 對 nums 進行排序
|
||||
val start = 0 // 走訪起始點
|
||||
val res: MutableList<List<Int>?> = ArrayList() // 結果串列(子集串列)
|
||||
val res = mutableListOf<MutableList<Int>?>() // 結果串列(子集串列)
|
||||
backtrack(state, target, nums, start, res)
|
||||
return res
|
||||
}
|
||||
@@ -57,7 +55,6 @@ fun subsetSumII(nums: IntArray, target: Int): List<List<Int>?> {
|
||||
fun main() {
|
||||
val nums = intArrayOf(4, 4, 5)
|
||||
val target = 9
|
||||
|
||||
val res = subsetSumII(nums, target)
|
||||
|
||||
println("輸入陣列 nums = ${nums.contentToString()}, target = $target")
|
||||
|
||||
Reference in New Issue
Block a user