From e1d561bc088b9f4623f06f3ba49420cc815ef56b Mon Sep 17 00:00:00 2001 From: danielsss Date: Sat, 7 Jan 2023 16:38:13 +1100 Subject: [PATCH 1/5] added: linear search for Typescript --- .../chapter_searching/linear_search.ts | 47 +++++++++++++++++++ codes/typescript/module/ListNode.ts | 15 ++++++ 2 files changed, 62 insertions(+) create mode 100644 codes/typescript/chapter_searching/linear_search.ts diff --git a/codes/typescript/chapter_searching/linear_search.ts b/codes/typescript/chapter_searching/linear_search.ts new file mode 100644 index 0000000000..05620f913d --- /dev/null +++ b/codes/typescript/chapter_searching/linear_search.ts @@ -0,0 +1,47 @@ +/** + * File: linear_search.ts + * Created Time: 2023-01-07 + * Author: Daniel (better.sunjian@gmail.com) + */ + +import ListNode from '../module/ListNode.ts'; + +/* 线性查找(数组) */ +function linearSearchArray(nums: number[], target: number): number { + // 遍历数组 + for (let i = 0; i < nums.length; i++) { + // 找到目标元素,返回其索引 + if (nums[i] === target) { + return i; + } + } + // 未找到目标元素,返回 -1 + return -1; +} + +/* 线性查找(链表)*/ +function linearSearchLinkedList(head: ListNode | null, target: number): ListNode | null { + // 遍历链表 + while (head) { + // 找到目标结点,返回之 + if (head.val === target) { + return head; + } + head = head.next; + } + // 未找到目标结点,返回 null + return null; +} + +/* Driver Code */ +const target = 3; + +/* 在数组中执行线性查找 */ +const nums = [ 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 ]; +const index = linearSearchArray(nums, target); +console.log('目标元素 3 的索引 =', index); + +/* 在链表中执行线性查找 */ +const head = ListNode.arrToLinkedList(nums); +const node = linearSearchLinkedList(head, target); +console.log('目标结点值 3 的对应结点对象为', node); diff --git a/codes/typescript/module/ListNode.ts b/codes/typescript/module/ListNode.ts index 17fdbb955c..84f1822f8e 100644 --- a/codes/typescript/module/ListNode.ts +++ b/codes/typescript/module/ListNode.ts @@ -14,4 +14,19 @@ export default class ListNode { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } + + /** + * Generate a linked-list with an array + * @param arr + * @return ListNode + */ + public static arrToLinkedList(arr: number[]): ListNode | null { + const dum = new ListNode(0); + let head = dum; + for (const val of arr) { + head.next = new ListNode(val); + head = head.next; + } + return dum.next; + } } From 2b0d7d1c1b1a9d1eda3efd6300782d4179ab27a7 Mon Sep 17 00:00:00 2001 From: danielsss Date: Sat, 7 Jan 2023 16:41:46 +1100 Subject: [PATCH 2/5] Added linear search docs for Typescript --- .../chapter_searching/linear_search.ts | 2 +- docs/chapter_searching/linear_search.md | 27 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/codes/typescript/chapter_searching/linear_search.ts b/codes/typescript/chapter_searching/linear_search.ts index 05620f913d..f0de33f788 100644 --- a/codes/typescript/chapter_searching/linear_search.ts +++ b/codes/typescript/chapter_searching/linear_search.ts @@ -6,7 +6,7 @@ import ListNode from '../module/ListNode.ts'; -/* 线性查找(数组) */ +/* 线性查找(数组)*/ function linearSearchArray(nums: number[], target: number): number { // 遍历数组 for (let i = 0; i < nums.length; i++) { diff --git a/docs/chapter_searching/linear_search.md b/docs/chapter_searching/linear_search.md index 3d592962e1..777d2d53bd 100644 --- a/docs/chapter_searching/linear_search.md +++ b/docs/chapter_searching/linear_search.md @@ -94,7 +94,18 @@ comments: true === "TypeScript" ```typescript title="linear_search.ts" - + /* 线性查找(数组)*/ + function linearSearchArray(nums: number[], target: number): number { + // 遍历数组 + for (let i = 0; i < nums.length; i++) { + // 找到目标元素,返回其索引 + if (nums[i] === target) { + return i; + } + } + // 未找到目标元素,返回 -1 + return -1; + } ``` === "C" @@ -210,7 +221,19 @@ comments: true === "TypeScript" ```typescript title="linear_search.ts" - + /* 线性查找(链表)*/ + function linearSearchLinkedList(head: ListNode | null, target: number): ListNode | null { + // 遍历链表 + while (head) { + // 找到目标结点,返回之 + if (head.val === target) { + return head; + } + head = head.next; + } + // 未找到目标结点,返回 null + return null; + } ``` === "C" From 1c3b4aea8bff7a0fcfe06e2ec56d8ce361b6a3a5 Mon Sep 17 00:00:00 2001 From: danielsss Date: Sat, 7 Jan 2023 16:46:18 +1100 Subject: [PATCH 3/5] Added .editorconfig for default code format --- .editorconfig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000000..3ccd9ac0c6 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,14 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true + +[codes/**.{java,js,ts,go,py,cpp,c,cs,swift,zig}] +charset = utf-8 +indent_style = space +indent_size = 2 From f29f6c460ee5bbd4dc91373730d6f02741eb15a1 Mon Sep 17 00:00:00 2001 From: danielsss Date: Sat, 7 Jan 2023 16:47:45 +1100 Subject: [PATCH 4/5] Added .editorconfig for default code format --- .editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 3ccd9ac0c6..f57b4f2fb9 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,4 +11,4 @@ insert_final_newline = true [codes/**.{java,js,ts,go,py,cpp,c,cs,swift,zig}] charset = utf-8 indent_style = space -indent_size = 2 +indent_size = 4 From afaf2de9f4f82a5452d7c290d2967c7db7378fd4 Mon Sep 17 00:00:00 2001 From: danielsss Date: Tue, 10 Jan 2023 21:46:03 +1100 Subject: [PATCH 5/5] removed: .editorconfig --- .editorconfig | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index f57b4f2fb9..0000000000 --- a/.editorconfig +++ /dev/null @@ -1,14 +0,0 @@ -# EditorConfig is awesome: https://EditorConfig.org - -# top-most EditorConfig file -root = true - -# Unix-style newlines with a newline ending every file -[*] -end_of_line = lf -insert_final_newline = true - -[codes/**.{java,js,ts,go,py,cpp,c,cs,swift,zig}] -charset = utf-8 -indent_style = space -indent_size = 4