Merge branch 'krahets:master' into master

This commit is contained in:
Daniel
2023-01-10 10:37:46 +11:00
committed by GitHub
37 changed files with 482 additions and 173 deletions
@@ -28,9 +28,9 @@ void remove(ListNode* n0) {
/* 访问链表中索引为 index 的结点 */
ListNode* access(ListNode* head, int index) {
for (int i = 0; i < index; i++) {
head = head->next;
if (head == nullptr)
return nullptr;
head = head->next;
}
return head;
}
@@ -8,9 +8,7 @@ namespace hello_algo.chapter_array_and_linkedlist
{
public class Array
{
/// <summary>
/// 随机返回一个数组元素
/// </summary>
/* 随机返回一个数组元素 */
public static int RandomAccess(int[] nums)
{
Random random = new();
@@ -19,9 +17,7 @@ namespace hello_algo.chapter_array_and_linkedlist
return randomNum;
}
/// <summary>
/// 扩展数组长度
/// </summary>
/* 扩展数组长度 */
public static int[] Extend(int[] nums, int enlarge)
{
// 初始化一个扩展长度后的数组
@@ -35,9 +31,7 @@ namespace hello_algo.chapter_array_and_linkedlist
return res;
}
/// <summary>
/// 在数组的索引 index 处插入元素 num
/// </summary>
/* 在数组的索引 index 处插入元素 num */
public static void Insert(int[] nums, int num, int index)
{
// 把索引 index 以及之后的所有元素向后移动一位
@@ -49,9 +43,7 @@ namespace hello_algo.chapter_array_and_linkedlist
nums[index] = num;
}
/// <summary>
/// 删除索引 index 处元素
/// </summary>
/* 删除索引 index 处元素 */
public static void Remove(int[] nums, int index)
{
// 把索引 index 之后的所有元素向前移动一位
@@ -61,9 +53,7 @@ namespace hello_algo.chapter_array_and_linkedlist
}
}
/// <summary>
/// 遍历数组
/// </summary>
/* 遍历数组 */
public static void Traverse(int[] nums)
{
int count = 0;
@@ -79,9 +69,7 @@ namespace hello_algo.chapter_array_and_linkedlist
}
}
/// <summary>
/// 在数组中查找指定元素
/// </summary>
/* 在数组中查找指定元素 */
public static int Find(int[] nums, int target)
{
for (int i = 0; i < nums.Length; i++)
@@ -92,15 +80,13 @@ namespace hello_algo.chapter_array_and_linkedlist
return -1;
}
/// <summary>
/// 辅助函数,数组转字符串
/// </summary>
/* 辅助函数,数组转字符串 */
public static string ToString(int[] nums)
{
return string.Join(",", nums);
}
// Driver Code
[Test]
public static void Test()
{
@@ -9,9 +9,7 @@ namespace hello_algo.chapter_array_and_linkedlist
{
public class linked_list
{
/// <summary>
/// 在链表的结点 n0 之后插入结点 P
/// </summary>
/* 在链表的结点 n0 之后插入结点 P */
public static void Insert(ListNode n0, ListNode P)
{
ListNode? n1 = n0.next;
@@ -19,9 +17,7 @@ namespace hello_algo.chapter_array_and_linkedlist
P.next = n1;
}
/// <summary>
/// 删除链表的结点 n0 之后的首个结点
/// </summary>
/* 删除链表的结点 n0 之后的首个结点 */
public static void Remove(ListNode n0)
{
if (n0.next == null)
@@ -32,23 +28,19 @@ namespace hello_algo.chapter_array_and_linkedlist
n0.next = n1;
}
/// <summary>
/// 访问链表中索引为 index 的结点
/// </summary>
/* 访问链表中索引为 index 的结点 */
public static ListNode? Access(ListNode head, int index)
{
for (int i = 0; i < index; i++)
{
head = head.next;
if (head == null)
return null;
head = head.next;
}
return head;
}
/// <summary>
/// 在链表中查找值为 target 的首个结点
/// </summary>
/* 在链表中查找值为 target 的首个结点 */
public static int Find(ListNode head, int target)
{
int index = 0;
@@ -62,7 +54,7 @@ namespace hello_algo.chapter_array_and_linkedlist
return -1;
}
// Driver Code
[Test]
public void Test()
{
@@ -35,11 +35,7 @@ namespace hello_algo.chapter_tree
return root;
}
/// <summary>
/// 查找结点
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
/* 查找结点 */
public TreeNode? search(int num)
{
TreeNode? cur = root;
+1 -5
View File
@@ -12,11 +12,7 @@ namespace hello_algo.chapter_tree
public class binary_tree_bfs
{
/// <summary>
/// 层序遍历
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
/* 层序遍历 */
public List<int> hierOrder(TreeNode root)
{
// 初始化队列,加入根结点
+3 -12
View File
@@ -13,10 +13,7 @@ namespace hello_algo.chapter_tree
{
List<int> list = new();
/// <summary>
/// 前序遍历
/// </summary>
/// <param name="root"></param>
/* 前序遍历 */
void preOrder(TreeNode? root)
{
if (root == null) return;
@@ -26,10 +23,7 @@ namespace hello_algo.chapter_tree
preOrder(root.right);
}
/// <summary>
/// 中序遍历
/// </summary>
/// <param name="root"></param>
/* 中序遍历 */
void inOrder(TreeNode? root)
{
if (root == null) return;
@@ -39,10 +33,7 @@ namespace hello_algo.chapter_tree
inOrder(root.right);
}
/// <summary>
/// 后序遍历
/// </summary>
/// <param name="root"></param>
/* 后序遍历 */
void postOrder(TreeNode? root)
{
if (root == null) return;
@@ -29,10 +29,10 @@ func removeNode(n0 *ListNode) {
/* 访问链表中索引为 index 的结点 */
func access(head *ListNode, index int) *ListNode {
for i := 0; i < index; i++ {
head = head.Next
if head == nil {
return nil
}
head = head.Next
}
return head
}
@@ -29,9 +29,9 @@ public class linked_list {
/* 访问链表中索引为 index 的结点 */
static ListNode access(ListNode head, int index) {
for (int i = 0; i < index; i++) {
head = head.next;
if (head == null)
return null;
head = head.next;
}
return head;
}
@@ -26,9 +26,9 @@ def remove(n0):
""" 访问链表中索引为 index 的结点 """
def access(head, index):
for _ in range(index):
head = head.next
if not head:
return None
head = head.next
return head
""" 在链表中查找值为 target 的首个结点 """
+4
View File
@@ -11,6 +11,8 @@ let package = Package(
.executable(name: "leetcode_two_sum", targets: ["leetcode_two_sum"]),
.executable(name: "array", targets: ["array"]),
.executable(name: "linked_list", targets: ["linked_list"]),
.executable(name: "list", targets: ["list"]),
.executable(name: "my_list", targets: ["my_list"]),
],
targets: [
.target(name: "utils", path: "utils"),
@@ -20,5 +22,7 @@ let package = Package(
.executableTarget(name: "leetcode_two_sum", path: "chapter_computational_complexity", sources: ["leetcode_two_sum.swift"]),
.executableTarget(name: "array", path: "chapter_array_and_linkedlist", sources: ["array.swift"]),
.executableTarget(name: "linked_list", dependencies: ["utils"], path: "chapter_array_and_linkedlist", sources: ["linked_list.swift"]),
.executableTarget(name: "list", path: "chapter_array_and_linkedlist", sources: ["list.swift"]),
.executableTarget(name: "my_list", path: "chapter_array_and_linkedlist", sources: ["my_list.swift"]),
]
)
@@ -29,10 +29,10 @@ func remove(n0: ListNode) {
func access(head: ListNode, index: Int) -> ListNode? {
var head: ListNode? = head
for _ in 0 ..< index {
head = head?.next
if head == nil {
return nil
}
head = head?.next
}
return head
}
@@ -0,0 +1,64 @@
/**
* File: list.swift
* Created Time: 2023-01-08
* Author: nuomi1 (nuomi1@qq.com)
*/
@main
enum List {
/* Driver Code */
static func main() {
/* */
var list = [1, 3, 2, 5, 4]
print("列表 list = \(list)")
/* 访 */
let num = list[1]
print("访问索引 1 处的元素,得到 num = \(num)")
/* */
list[1] = 0
print("将索引 1 处的元素更新为 0 ,得到 list = \(list)")
/* */
list.removeAll()
print("清空列表后 list = \(list)")
/* */
list.append(1)
list.append(3)
list.append(2)
list.append(5)
list.append(4)
print("添加元素后 list = \(list)")
/* */
list.insert(6, at: 3)
print("在索引 3 处插入数字 6 ,得到 list = \(list)")
/* */
list.remove(at: 3)
print("删除索引 3 处的元素,得到 list = \(list)")
/* */
var count = 0
for _ in list.indices {
count += 1
}
/* */
count = 0
for _ in list {
count += 1
}
/* */
let list1 = [6, 8, 7, 10, 9]
list.append(contentsOf: list1)
print("将列表 list1 拼接到 list 之后,得到 list = \(list)")
/* */
list.sort()
print("排序列表后 list = \(list)")
}
}
@@ -0,0 +1,147 @@
/**
* File: my_list.swift
* Created Time: 2023-01-08
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
class MyList {
private var nums: [Int] //
private var _capacity = 10 //
private var _size = 0 //
private let extendRatio = 2 //
/* */
init() {
nums = Array(repeating: 0, count: _capacity)
}
/* */
func size() -> Int {
_size
}
/* */
func capacity() -> Int {
_capacity
}
/* 访 */
func get(index: Int) -> Int {
//
if index >= _size {
fatalError("索引越界")
}
return nums[index]
}
/* */
func set(index: Int, num: Int) {
if index >= _size {
fatalError("索引越界")
}
nums[index] = num
}
/* */
func add(num: Int) {
//
if _size == _capacity {
extendCapacity()
}
nums[_size] = num
//
_size += 1
}
/* */
func insert(index: Int, num: Int) {
if index >= _size {
fatalError("索引越界")
}
//
if _size == _capacity {
extendCapacity()
}
// index
for j in sequence(first: _size - 1, next: { $0 >= index + 1 ? $0 - 1 : nil }) {
nums[j + 1] = nums[j]
}
nums[index] = num
//
_size += 1
}
/* */
@discardableResult
func remove(index: Int) -> Int {
if index >= _size {
fatalError("索引越界")
}
let num = nums[index]
// index
for j in index ..< (_size - 1) {
nums[j] = nums[j + 1]
}
//
_size -= 1
//
return num
}
/* */
func extendCapacity() {
// size
nums = nums + Array(repeating: 0, count: _capacity * (extendRatio - 1))
//
_capacity = nums.count
}
/* */
func toArray() -> [Int] {
var nums = Array(repeating: 0, count: _size)
for i in 0 ..< _size {
nums[i] = get(index: i)
}
return nums
}
}
@main
enum _MyList {
/* Driver Code */
static func main() {
/* */
let list = MyList()
/* */
list.add(num: 1)
list.add(num: 3)
list.add(num: 2)
list.add(num: 5)
list.add(num: 4)
print("列表 list = \(list.toArray()) ,容量 = \(list.capacity()) ,长度 = \(list.size())")
/* */
list.insert(index: 3, num: 6)
print("在索引 3 处插入数字 6 ,得到 list = \(list.toArray())")
/* */
list.remove(index: 3)
print("删除索引 3 处的元素,得到 list = \(list.toArray())")
/* 访 */
let num = list.get(index: 1)
print("访问索引 1 处的元素,得到 num = \(num)")
/* */
list.set(index: 1, num: 0)
print("将索引 1 处的元素更新为 0 ,得到 list = \(list.toArray())")
/* */
for i in 0 ..< 10 {
// i = 5
list.add(num: i)
}
print("扩容后的列表 list = \(list.toArray()) ,容量 = \(list.capacity()) ,长度 = \(list.size())")
}
}
@@ -49,7 +49,7 @@ func quadratic(n: Int) -> Int {
func bubbleSort(nums: inout [Int]) -> Int {
var count = 0 //
// n-1, n-2, ..., 1
for i in sequence(first: nums.count - 1, next: { $0 > 0 ? $0 - 1 : nil }) {
for i in sequence(first: nums.count - 1, next: { $0 > 0 + 1 ? $0 - 1 : nil }) {
//
for j in 0 ..< i {
if nums[j] > nums[j + 1] {
@@ -149,7 +149,7 @@ enum TimeComplexity {
count = quadratic(n: n)
print("平方阶的计算操作数量 = \(count)")
var nums = Array(sequence(first: n, next: { $0 > 0 ? $0 - 1 : nil })) // [n,n-1,...,2,1]
var nums = Array(sequence(first: n, next: { $0 > 0 + 1 ? $0 - 1 : nil })) // [n,n-1,...,2,1]
count = bubbleSort(nums: &nums)
print("平方阶(冒泡排序)的计算操作数量 = \(count)")