Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add PHP Codes for chapter_computational_complexity, chapter_array_and_linkedlist, chapter_stack_and_queue #1369

Closed
wants to merge 4 commits into from

Conversation

DoWake
Copy link

@DoWake DoWake commented May 19, 2024

If this pull request (PR) is associated with coding or code transpilation, please attach the relevant console outputs to the PR and complete the following checklist:

  • I have thoroughly reviewed the code, focusing on its formatting, comments, indentation, and file headers.
  • I have confirmed that the code execution outputs are consistent with those produced by the reference code (Python or Java).
  • The code is designed to be compatible on standard operating systems, including Windows, macOS, and Ubuntu.

The terminal output of each file is as follows:

chapter_computational_complexity

iteration.php


for 循环的求和结果 res = 15

while 循环的求和结果 res = 15

while 循环(两次更新)求和结果 res = 5

双层 for 循环的遍历结果 (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5),

recursion.php


递归函数的求和结果 res = 15

使用迭代模拟递归求和结果 res = 15

尾递归函数的求和结果 res = 15

斐波那契数列的第 5 项为 3

space_complexity.php

递归 n = 5
递归 n = 4
递归 n = 3
递归 n = 2
递归 n = 1
递归 n = 5 中的 nums 长度 = 5
递归 n = 4 中的 nums 长度 = 4
递归 n = 3 中的 nums 长度 = 3
递归 n = 2 中的 nums 长度 = 2
递归 n = 1 中的 nums 长度 = 1
                /——— 0
            /——— 0
           |    \——— 0
        /——— 0
       |   |    /——— 0
       |    \——— 0
       |        \——— 0
    /——— 0
   |   |        /——— 0
   |   |    /——— 0
   |   |   |    \——— 0
   |    \——— 0
   |       |    /——— 0
   |        \——— 0
   |            \——— 0
——— 0
   |            /——— 0
   |        /——— 0
   |       |    \——— 0
   |    /——— 0
   |   |   |    /——— 0
   |   |    \——— 0
   |   |        \——— 0
    \——— 0
       |        /——— 0
       |    /——— 0
       |   |    \——— 0
        \——— 0
           |    /——— 0
            \——— 0
                \——— 0

time_complexity.php

输入数据大小 n = 8
常数阶的操作数量 = 100000
线性阶的操作数量 = 8
线性阶(遍历数组)的操作数量 = 8
平方阶的操作数量 = 64
平方阶(冒泡排序)的操作数量 = 84
指数阶(循环实现)的操作数量 = 255
指数阶(递归实现)的操作数量 = 255
对数阶(循环实现)的操作数量 = 3
对数阶(递归实现)的操作数量 = 3
线性对数阶(递归实现)的操作数量 = 32
阶乘阶(递归实现)的操作数量 = 40320

worst_best_time_complexity.php


数组 [ 1, 2, ..., n ] 被打乱后 = [26, 90, 24, 16, 51, 44, 47, 13, 49, 23, 95, 52, 28, 39, 3, 45, 50, 8, 34, 36, 20, 46, 18, 76, 31, 64, 37, 4, 1, 5, 67, 33, 35, 54, 65, 9, 97, 53, 2, 27, 10, 89, 75, 21, 74, 92, 15, 61, 22, 87, 69, 12, 93, 83, 73, 42, 14, 19, 17, 62, 70, 41, 84, 72, 98, 59, 43, 68, 48, 57, 60, 32, 56, 30, 71, 40, 85, 38, 77, 81, 88, 82, 100, 78, 7, 91, 86, 99, 63, 25, 94, 66, 80, 55, 58, 29, 96, 11, 6, 79]
数字 1 的索引为 28

数组 [ 1, 2, ..., n ] 被打乱后 = [90, 77, 56, 92, 33, 87, 9, 2, 86, 35, 54, 81, 64, 51, 6, 42, 20, 66, 68, 5, 93, 13, 19, 88, 26, 89, 28, 75, 8, 65, 46, 31, 10, 4, 76, 67, 83, 32, 59, 47, 91, 58, 48, 94, 61, 62, 21, 22, 63, 17, 43, 98, 73, 50, 34, 82, 95, 97, 23, 80, 41, 7, 85, 53, 37, 49, 74, 12, 70, 27, 40, 84, 15, 1, 96, 72, 69, 38, 71, 44, 16, 39, 60, 52, 24, 3, 29, 14, 78, 100, 30, 99, 18, 36, 57, 45, 25, 79, 11, 55]
数字 1 的索引为 73

数组 [ 1, 2, ..., n ] 被打乱后 = [91, 71, 55, 98, 76, 54, 32, 47, 68, 2, 24, 36, 25, 10, 72, 44, 60, 94, 99, 73, 83, 35, 3, 67, 11, 92, 12, 52, 62, 48, 50, 45, 6, 28, 90, 38, 77, 18, 93, 13, 82, 15, 23, 85, 70, 37, 89, 51, 69, 20, 31, 46, 29, 57, 96, 19, 53, 26, 78, 39, 17, 41, 5, 9, 34, 16, 49, 75, 64, 43, 56, 42, 33, 74, 100, 86, 65, 87, 58, 14, 66, 80, 84, 27, 63, 7, 30, 22, 8, 95, 59, 81, 21, 61, 4, 97, 79, 1, 88, 40]
数字 1 的索引为 97

数组 [ 1, 2, ..., n ] 被打乱后 = [19, 2, 31, 54, 17, 81, 44, 38, 99, 76, 21, 23, 3, 6, 100, 11, 8, 36, 35, 39, 52, 5, 24, 93, 64, 37, 56, 16, 4, 74, 94, 68, 98, 78, 40, 59, 86, 75, 25, 55, 41, 96, 51, 32, 45, 91, 20, 14, 87, 29, 61, 42, 80, 85, 71, 28, 72, 30, 60, 88, 49, 22, 89, 18, 1, 57, 13, 82, 26, 63, 33, 70, 12, 65, 84, 73, 77, 83, 9, 97, 62, 50, 58, 46, 27, 48, 34, 66, 90, 69, 10, 7, 92, 15, 95, 53, 67, 43, 79, 47]
数字 1 的索引为 64

数组 [ 1, 2, ..., n ] 被打乱后 = [73, 1, 12, 56, 41, 98, 26, 48, 4, 53, 50, 33, 77, 55, 9, 22, 51, 83, 85, 65, 49, 5, 27, 59, 90, 82, 10, 39, 45, 70, 62, 38, 24, 2, 92, 42, 16, 17, 23, 94, 52, 54, 20, 46, 84, 78, 31, 47, 99, 11, 72, 18, 35, 71, 67, 74, 14, 86, 91, 57, 3, 87, 28, 89, 66, 100, 44, 63, 43, 58, 88, 19, 79, 37, 81, 30, 60, 61, 6, 21, 15, 75, 40, 80, 68, 7, 69, 32, 95, 8, 25, 76, 93, 36, 29, 13, 97, 96, 64, 34]
数字 1 的索引为 1

数组 [ 1, 2, ..., n ] 被打乱后 = [42, 9, 21, 74, 4, 65, 91, 20, 50, 43, 85, 12, 18, 69, 2, 99, 64, 5, 93, 22, 100, 57, 55, 82, 54, 73, 58, 14, 1, 71, 60, 78, 51, 81, 37, 84, 13, 80, 32, 90, 68, 31, 45, 96, 47, 49, 41, 11, 44, 25, 33, 19, 16, 23, 52, 59, 72, 66, 63, 95, 56, 67, 27, 26, 76, 6, 53, 39, 48, 36, 87, 28, 94, 24, 30, 17, 15, 10, 86, 29, 3, 77, 98, 34, 88, 7, 8, 89, 62, 38, 61, 83, 79, 92, 40, 75, 70, 35, 97, 46]
数字 1 的索引为 28

数组 [ 1, 2, ..., n ] 被打乱后 = [97, 63, 51, 53, 65, 43, 18, 27, 20, 64, 82, 96, 90, 85, 10, 81, 49, 6, 22, 52, 40, 1, 46, 12, 16, 42, 28, 13, 31, 98, 35, 41, 89, 36, 99, 67, 80, 66, 92, 33, 47, 84, 32, 5, 23, 8, 3, 88, 59, 50, 45, 71, 94, 62, 57, 60, 87, 26, 14, 83, 73, 30, 58, 75, 93, 78, 44, 56, 86, 11, 76, 29, 21, 55, 77, 17, 25, 24, 19, 68, 37, 69, 100, 72, 4, 2, 7, 9, 61, 39, 34, 38, 74, 91, 15, 48, 54, 79, 95, 70]
数字 1 的索引为 21

数组 [ 1, 2, ..., n ] 被打乱后 = [89, 42, 69, 30, 88, 25, 78, 68, 31, 6, 3, 32, 35, 29, 61, 72, 81, 48, 49, 55, 18, 76, 82, 34, 80, 79, 22, 62, 39, 16, 63, 13, 73, 47, 84, 21, 75, 4, 15, 99, 65, 100, 93, 36, 92, 51, 5, 59, 66, 50, 27, 17, 9, 97, 54, 70, 12, 28, 40, 2, 53, 33, 41, 56, 24, 7, 94, 57, 26, 8, 43, 85, 71, 87, 20, 45, 60, 90, 19, 14, 77, 64, 38, 37, 86, 10, 1, 58, 44, 98, 91, 23, 96, 95, 67, 11, 52, 74, 46, 83]
数字 1 的索引为 86

数组 [ 1, 2, ..., n ] 被打乱后 = [44, 37, 50, 9, 55, 48, 77, 76, 90, 67, 29, 36, 27, 75, 70, 26, 60, 74, 15, 43, 16, 34, 65, 5, 14, 53, 93, 11, 96, 86, 61, 54, 46, 73, 10, 2, 99, 49, 18, 68, 80, 12, 81, 58, 83, 4, 45, 71, 98, 31, 64, 39, 78, 51, 100, 22, 7, 69, 82, 17, 84, 56, 3, 92, 88, 47, 59, 6, 57, 42, 85, 23, 72, 41, 89, 94, 13, 24, 87, 21, 63, 97, 20, 8, 95, 19, 66, 33, 28, 38, 62, 91, 52, 40, 25, 1, 32, 30, 35, 79]
数字 1 的索引为 95

数组 [ 1, 2, ..., n ] 被打乱后 = [74, 95, 87, 88, 44, 72, 80, 31, 50, 22, 84, 92, 35, 89, 38, 60, 19, 41, 18, 83, 76, 9, 99, 78, 77, 24, 23, 1, 13, 69, 39, 64, 53, 90, 27, 43, 48, 11, 67, 40, 66, 86, 62, 82, 68, 65, 100, 56, 4, 12, 94, 81, 70, 14, 71, 75, 63, 16, 8, 45, 6, 52, 3, 93, 51, 15, 30, 17, 2, 98, 7, 25, 59, 73, 37, 97, 33, 85, 61, 32, 96, 47, 34, 29, 49, 10, 21, 46, 20, 57, 55, 54, 91, 26, 79, 28, 5, 36, 42, 58]
数字 1 的索引为 27

chapter_array_and_linkedlist

array.php

数组 arr = [0, 0, 0, 0, 0]
数组 nums = [1, 3, 2, 5, 4]
在 nums 中获取随机元素 2
将数组长度扩展至 8 ,得到 nums = [1, 3, 2, 5, 4, 0, 0, 0]
在索引 3 处插入数字 6 ,得到 nums = [1, 3, 2, 6, 5, 4, 0, 0]
删除索引 2 处的元素,得到 nums = [1, 3, 6, 5, 4, 0, 0, 0]
在 nums 中查找元素 3 ,得到索引 = 1

linked_list.php

初始化的链表为
1 -> 3 -> 2 -> 5 -> 4
插入节点后的链表为
1 -> 0 -> 3 -> 2 -> 5 -> 4
删除节点后的链表为
1 -> 3 -> 2 -> 5 -> 4
链表中索引 3 处的节点的值 = 5
链表中值为 2 的节点的索引 = 2

list.php

列表 nums = [1, 3, 2, 5, 4]
访问索引 1 处的元素,得到 num = 3
将索引 1 处的元素更新为 0 ,得到 nums = [1, 0, 2, 5, 4]
清空列表后 nums = []
添加元素后 nums = [1, 3, 2, 5, 4]
在索引 3 处插入数字 6 ,得到 nums = [1, 3, 2, 6, 5, 4]
删除索引 3 处的元素,得到 nums = [1, 3, 2, 5, 4]
将列表 nums1 拼接到 nums 之后,得到 nums = [1, 3, 2, 5, 4, 6, 8, 7, 10, 9]
排序列表后 nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

my_list.php

列表 nums = [1, 3, 2, 5, 4] ,容量 = 10 ,长度 = 5
在索引 3 处插入数字 6 ,得到 nums = [1, 3, 2, 6, 5, 4]
删除索引 3 处的元素,得到 nums = [1, 3, 2, 5, 4]
访问索引 1 处的元素,得到 num = 3
将索引 1 处的元素更新为 0 ,得到 nums = [1, 0, 2, 5, 4]
扩容后的列表 nums = [1, 0, 2, 5, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ,容量 = 20 ,长度 = 15

chapter_stack_and_queue

array_deque.php

双向队列 deque = [3, 2, 5]
队首元素 peekFirst = 3
队尾元素 peekLast = 5
元素 4 队尾入队后 deque = [3, 2, 5, 4]
元素 1 队首入队后 deque = [1, 3, 2, 5, 4]
队尾出队元素 = 4,队尾出队后 deque = [1, 3, 2, 5]
队首出队元素 = 1,队首出队后 deque = [3, 2, 5]
双向队列长度 size = 3
双向队列是否为空 = false

array_queue.php

队列 queue = [1, 3, 2, 5, 4]
队首元素 peek = 1
出队元素 pop = 1,出队后 queue = [3, 2, 5, 4]
队列长度 size = 4
队列是否为空 = false
第 0 轮入队 + 出队后 queue = [2, 5, 4, 0]
第 1 轮入队 + 出队后 queue = [5, 4, 0, 1]
第 2 轮入队 + 出队后 queue = [4, 0, 1, 2]
第 3 轮入队 + 出队后 queue = [0, 1, 2, 3]
第 4 轮入队 + 出队后 queue = [1, 2, 3, 4]
第 5 轮入队 + 出队后 queue = [2, 3, 4, 5]
第 6 轮入队 + 出队后 queue = [3, 4, 5, 6]
第 7 轮入队 + 出队后 queue = [4, 5, 6, 7]
第 8 轮入队 + 出队后 queue = [5, 6, 7, 8]
第 9 轮入队 + 出队后 queue = [6, 7, 8, 9]

array_stack.php

栈 stack = [1, 3, 2, 5, 4]
栈顶元素 peek = 4
出栈元素 pop = 4,出栈后 stack = [1, 3, 2, 5]
栈的长度 size = 4
栈是否为空 = false

deque.php

双向队列 deque = [3, 2, 5]
队首元素 peekFirst = 3
队尾元素 peekLast = 5
元素 4 队尾入队后 deque = [3, 2, 5, 4]
元素 1 队首入队后 deque = [1, 3, 2, 5, 4]
队尾出队元素 = 4,队尾出队后 deque = [1, 3, 2, 5]
队首出队元素 = 1,队首出队后 deque = [3, 2, 5]
双向队列长度 size = 3
双向队列是否为空 = false

linkedlist_deque.php

双向队列 deque = [3, 2, 5]
队首元素 peekFirst = 3
队尾元素 peekLast = 5
元素 4 队尾入队后 deque = [3, 2, 5, 4]
元素 1 队首入队后 deque = [1, 3, 2, 5, 4]
队尾出队元素 = 4,队尾出队后 deque = [1, 3, 2, 5]
队首出队元素 = 1,队首出队后 deque = [3, 2, 5]
双向队列长度 size = 3
双向队列是否为空 = false

linkedlist_queue.php

队列 queue = [1, 3, 2, 5, 4]
队首元素 peek = 1
出队元素 pop = 1,出队后 queue = [3, 2, 5, 4]
队列长度 size = 4
队列是否为空 = false

linkedlist_stack.php

栈 stack = [1, 3, 2, 5, 4]
栈顶元素 peek = 4
出栈元素 pop = 4,出栈后 stack = [1, 3, 2, 5]
栈的长度 size = 4
栈是否为空 = false

queue.php

队列 queue = [1, 3, 2, 5, 4]
队首元素 peek = 1
出队元素 pop = 1,出队后 queue = [3, 2, 5, 4]
队列长度 size = 4
队列是否为空 = false

stack.php

栈 stack = [1, 3, 2, 5, 4]
栈顶元素 peek = 4
出栈元素 pop = 4,出栈后 stack = [1, 3, 2, 5]
栈的长度 size = 4
栈是否为空 = false

@krahets
Copy link
Owner

krahets commented May 20, 2024

Hi @DoWake, thanks for the PR! Does the code follow the PHP Code Style Guide?

I suggest you submit your work by chapter. This PR is too large to review.

@DoWake
Copy link
Author

DoWake commented May 23, 2024

Hi @DoWake, thanks for the PR! Does the code follow the PHP Code Style Guide?

I suggest you submit your work by chapter. This PR is too large to review.

Firstly, thank you very much for your suggestion. There are several coding standards in PHP that are established by PHP-FIG and widely accepted. I acknowledge that there are currently some issues with the coding standards in my code, and I apologize for that. Moving forward, I will continue to improve and refine my code.

@DoWake DoWake closed this May 23, 2024
@DoWake DoWake deleted the php branch May 23, 2024 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants