Skip to content

Commit

Permalink
style(kotlin): value -> _val
Browse files Browse the repository at this point in the history
  • Loading branch information
curtishd committed Apr 9, 2024
1 parent 7e88864 commit ab0cd5c
Show file tree
Hide file tree
Showing 28 changed files with 152 additions and 152 deletions.
4 changes: 2 additions & 2 deletions codes/kotlin/chapter_array_and_linkedlist/linked_list.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fun find(head: ListNode?, target: Int): Int {
var index = 0
var h = head
while (h != null) {
if (h.value == target)
if (h._val == target)
return index
h = h.next
index++
Expand Down Expand Up @@ -79,7 +79,7 @@ fun main() {

/* 访问节点 */
val node: ListNode = access(n0, 3)!!
println("链表中索引 3 处的节点的值 = ${node.value}")
println("链表中索引 3 处的节点的值 = ${node._val}")

/* 查找节点 */
val index: Int = find(n0, 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fun preOrder(root: TreeNode?) {
if (root == null) {
return
}
if (root.value == 7) {
if (root._val == 7) {
// 记录解
res!!.add(root)
}
Expand All @@ -37,7 +37,7 @@ fun main() {
println("\n输出所有值为 7 的节点")
val vals = mutableListOf<Int>()
for (node in res!!) {
vals.add(node.value)
vals.add(node._val)
}
println(vals)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fun preOrder(root: TreeNode?) {
}
// 尝试
path!!.add(root)
if (root.value == 7) {
if (root._val == 7) {
// 记录解
res!!.add(path!!.toMutableList())
}
Expand All @@ -42,10 +42,10 @@ fun main() {

println("\n输出所有根节点到节点 7 的路径")
for (path in res!!) {
val values = mutableListOf<Int>()
val _vals = mutableListOf<Int>()
for (node in path) {
values.add(node.value)
_vals.add(node._val)
}
println(values)
println(_vals)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ 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(path!!.toMutableList())
}
Expand All @@ -43,10 +43,10 @@ fun main() {

println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点")
for (path in res!!) {
val values = mutableListOf<Int>()
val _vals = mutableListOf<Int>()
for (node in path) {
values.add(node.value)
_vals.add(node._val)
}
println(values)
println(_vals)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import utils.printTree

/* 判断当前状态是否为解 */
fun isSolution(state: MutableList<TreeNode?>): Boolean {
return state.isNotEmpty() && state[state.size - 1]?.value == 7
return state.isNotEmpty() && state[state.size - 1]?._val == 7
}

/* 记录解 */
Expand All @@ -21,7 +21,7 @@ fun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<MutableList<

/* 判断在当前状态下,该选择是否合法 */
fun isValid(state: MutableList<TreeNode?>?, choice: TreeNode?): Boolean {
return choice != null && choice.value != 3
return choice != null && choice._val != 3
}

/* 更新状态 */
Expand Down Expand Up @@ -74,7 +74,7 @@ fun main() {
val vals = mutableListOf<Int>()
for (node in path!!) {
if (node != null) {
vals.add(node.value)
vals.add(node._val)
}
}
println(vals)
Expand Down
34 changes: 17 additions & 17 deletions codes/kotlin/chapter_dynamic_programming/knapsack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlin.math.max
/* 0-1 背包:暴力搜索 */
fun knapsackDFS(
wgt: IntArray,
value: IntArray,
_val: IntArray,
i: Int,
c: Int
): Int {
Expand All @@ -21,19 +21,19 @@ fun knapsackDFS(
}
// 若超过背包容量,则只能选择不放入背包
if (wgt[i - 1] > c) {
return knapsackDFS(wgt, value, i - 1, c)
return knapsackDFS(wgt, _val, i - 1, c)
}
// 计算不放入和放入物品 i 的最大价值
val no = knapsackDFS(wgt, value, i - 1, c)
val yes = knapsackDFS(wgt, value, i - 1, c - wgt[i - 1]) + value[i - 1]
val no = knapsackDFS(wgt, _val, i - 1, c)
val yes = knapsackDFS(wgt, _val, i - 1, c - wgt[i - 1]) + _val[i - 1]
// 返回两种方案中价值更大的那一个
return max(no, yes)
}

/* 0-1 背包:记忆化搜索 */
fun knapsackDFSMem(
wgt: IntArray,
value: IntArray,
_val: IntArray,
mem: Array<IntArray>,
i: Int,
c: Int
Expand All @@ -48,11 +48,11 @@ fun knapsackDFSMem(
}
// 若超过背包容量,则只能选择不放入背包
if (wgt[i - 1] > c) {
return knapsackDFSMem(wgt, value, mem, i - 1, c)
return knapsackDFSMem(wgt, _val, mem, i - 1, c)
}
// 计算不放入和放入物品 i 的最大价值
val no = knapsackDFSMem(wgt, value, mem, i - 1, c)
val yes = knapsackDFSMem(wgt, value, mem, i - 1, c - wgt[i - 1]) + value[i - 1]
val no = knapsackDFSMem(wgt, _val, mem, i - 1, c)
val yes = knapsackDFSMem(wgt, _val, mem, i - 1, c - wgt[i - 1]) + _val[i - 1]
// 记录并返回两种方案中价值更大的那一个
mem[i][c] = max(no, yes)
return mem[i][c]
Expand All @@ -61,7 +61,7 @@ fun knapsackDFSMem(
/* 0-1 背包:动态规划 */
fun knapsackDP(
wgt: IntArray,
value: IntArray,
_val: IntArray,
cap: Int
): Int {
val n = wgt.size
Expand All @@ -75,7 +75,7 @@ fun knapsackDP(
dp[i][c] = dp[i - 1][c]
} else {
// 不选和选物品 i 这两种方案的较大值
dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + value[i - 1])
dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + _val[i - 1])
}
}
}
Expand All @@ -85,7 +85,7 @@ fun knapsackDP(
/* 0-1 背包:空间优化后的动态规划 */
fun knapsackDPComp(
wgt: IntArray,
value: IntArray,
_val: IntArray,
cap: Int
): Int {
val n = wgt.size
Expand All @@ -98,7 +98,7 @@ fun knapsackDPComp(
if (wgt[i - 1] <= c) {
// 不选和选物品 i 这两种方案的较大值
dp[c] =
max(dp[c], dp[c - wgt[i - 1]] + value[i - 1])
max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])
}
}
}
Expand All @@ -108,27 +108,27 @@ fun knapsackDPComp(
/* Driver Code */
fun main() {
val wgt = intArrayOf(10, 20, 30, 40, 50)
val value = intArrayOf(50, 120, 150, 210, 240)
val _val = intArrayOf(50, 120, 150, 210, 240)
val cap = 50
val n = wgt.size

// 暴力搜索
var res = knapsackDFS(wgt, value, n, cap)
var res = knapsackDFS(wgt, _val, n, cap)
println("不超过背包容量的最大物品价值为 $res")

// 记忆化搜索
val mem = Array(n + 1) { IntArray(cap + 1) }
for (row in mem) {
row.fill(-1)
}
res = knapsackDFSMem(wgt, value, mem, n, cap)
res = knapsackDFSMem(wgt, _val, mem, n, cap)
println("不超过背包容量的最大物品价值为 $res")

// 动态规划
res = knapsackDP(wgt, value, cap)
res = knapsackDP(wgt, _val, cap)
println("不超过背包容量的最大物品价值为 $res")

// 空间优化后的动态规划
res = knapsackDPComp(wgt, value, cap)
res = knapsackDPComp(wgt, _val, cap)
println("不超过背包容量的最大物品价值为 $res")
}
14 changes: 7 additions & 7 deletions codes/kotlin/chapter_dynamic_programming/unbounded_knapsack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package chapter_dynamic_programming
import kotlin.math.max

/* 完全背包:动态规划 */
fun unboundedKnapsackDP(wgt: IntArray, value: IntArray, cap: Int): Int {
fun unboundedKnapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {
val n = wgt.size
// 初始化 dp 表
val dp = Array(n + 1) { IntArray(cap + 1) }
Expand All @@ -21,7 +21,7 @@ fun unboundedKnapsackDP(wgt: IntArray, value: IntArray, cap: Int): Int {
dp[i][c] = dp[i - 1][c]
} else {
// 不选和选物品 i 这两种方案的较大值
dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + value[i - 1])
dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + _val[i - 1])
}
}
}
Expand All @@ -31,7 +31,7 @@ fun unboundedKnapsackDP(wgt: IntArray, value: IntArray, cap: Int): Int {
/* 完全背包:空间优化后的动态规划 */
fun unboundedKnapsackDPComp(
wgt: IntArray,
value: IntArray,
_val: IntArray,
cap: Int
): Int {
val n = wgt.size
Expand All @@ -45,7 +45,7 @@ fun unboundedKnapsackDPComp(
dp[c] = dp[c]
} else {
// 不选和选物品 i 这两种方案的较大值
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + value[i - 1])
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])
}
}
}
Expand All @@ -55,14 +55,14 @@ fun unboundedKnapsackDPComp(
/* Driver Code */
fun main() {
val wgt = intArrayOf(1, 2, 3)
val value = intArrayOf(5, 11, 15)
val _val = intArrayOf(5, 11, 15)
val cap = 4

// 动态规划
var res = unboundedKnapsackDP(wgt, value, cap)
var res = unboundedKnapsackDP(wgt, _val, cap)
println("不超过背包容量的最大物品价值为 $res")

// 空间优化后的动态规划
res = unboundedKnapsackDPComp(wgt, value, cap)
res = unboundedKnapsackDPComp(wgt, _val, cap)
println("不超过背包容量的最大物品价值为 $res")
}
4 changes: 2 additions & 2 deletions codes/kotlin/chapter_graph/graph_adjacency_list.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
for (pair in adjList.entries) {
val tmp = mutableListOf<Int>()
for (vertex in pair.value) {
tmp.add(vertex.value)
tmp.add(vertex._val)
}
println("${pair.key.value}: $tmp,")
println("${pair.key._val}: $tmp,")
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions codes/kotlin/chapter_graph/graph_adjacency_matrix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
}

/* 添加顶点 */
fun addVertex(value: Int) {
fun addVertex(_val: Int) {
val n = size()
// 向顶点列表中添加新顶点的值
vertices.add(value)
vertices.add(_val)
// 在邻接矩阵中添加一行
val newRow = mutableListOf<Int>()
for (j in 0..<n) {
Expand Down
4 changes: 2 additions & 2 deletions codes/kotlin/chapter_greedy/fractional_knapsack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ fun fractionalKnapsack(wgt: IntArray, _val: IntArray, c: Int): Double {
/* Driver Code */
fun main() {
val wgt = intArrayOf(10, 20, 30, 40, 50)
val values = intArrayOf(50, 120, 150, 210, 240)
val _val = intArrayOf(50, 120, 150, 210, 240)
val cap = 50

// 贪心算法
val res = fractionalKnapsack(wgt, values, cap)
val res = fractionalKnapsack(wgt, _val, cap)
println("不超过背包容量的最大物品价值为 $res")
}
20 changes: 10 additions & 10 deletions codes/kotlin/chapter_hashing/array_hash_map.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package chapter_hashing
/* 键值对 */
class Pair(
var key: Int,
var value: String
var _val: String
)

/* 基于数组实现的哈希表 */
Expand All @@ -27,12 +27,12 @@ class ArrayHashMap {
fun get(key: Int): String? {
val index = hashFunc(key)
val pair = buckets[index] ?: return null
return pair.value
return pair._val
}

/* 添加操作 */
fun put(key: Int, value: String) {
val pair = Pair(key, value)
fun put(key: Int, _val: String) {
val pair = Pair(key, _val)
val index = hashFunc(key)
buckets[index] = pair
}
Expand Down Expand Up @@ -68,7 +68,7 @@ class ArrayHashMap {
fun valueSet(): MutableList<String> {
val valueSet = mutableListOf<String>()
for (pair in buckets) {
pair?.let { valueSet.add(it.value) }
pair?.let { valueSet.add(it._val) }
}
return valueSet
}
Expand All @@ -77,8 +77,8 @@ class ArrayHashMap {
fun print() {
for (kv in pairSet()) {
val key = kv.key
val value = kv.value
println("${key} -> ${value}")
val _val = kv._val
println("${key} -> ${_val}")
}
}
}
Expand Down Expand Up @@ -112,14 +112,14 @@ fun main() {
/* 遍历哈希表 */
println("\n遍历键值对 Key -> Value")
for (kv in map.pairSet()) {
println("${kv.key} -> ${kv.value}")
println("${kv.key} -> ${kv._val}")
}
println("\n单独遍历键 Key")
for (key in map.keySet()) {
println(key)
}
println("\n单独遍历值 Value")
for (value in map.valueSet()) {
println(value)
for (_val in map.valueSet()) {
println(_val)
}
}
Loading

0 comments on commit ab0cd5c

Please sign in to comment.