From a4f22fb1898225387cf905053e8b5d2d04822c44 Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Mon, 31 May 2021 20:33:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20ts=20solution=20to=20lcof=20probl?= =?UTF-8?q?em:=20No.03.=20=E6=95=B0=E7=BB=84=E4=B8=AD=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 1 + .../README.md" | 19 +++++++++++++++++++ .../Solution.ts" | 14 ++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/Solution.ts" diff --git a/index.html b/index.html index ad9402cd6a09f..f17e60f55a9e9 100644 --- a/index.html +++ b/index.html @@ -103,6 +103,7 @@ + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/README.md" index 2c384815a25e2..53bd909609998 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/README.md" @@ -144,6 +144,25 @@ public: }; ``` +### **TypeScript** + +```ts +function findRepeatNumber(nums: number[]): number { + let n: number = nums.length; + for (let i: number = 0; i < n; i++) { + while (nums[i] != i) { + if (nums[i] == nums[nums[i]]) return nums[i]; + swap(nums, i, nums[i]); + } + } + return -1; +}; + +function swap (nums: number[], i: number, j: number): void { + [nums[i], nums[j]] = [nums[j], nums[i]]; +} +``` + ### **...** ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/Solution.ts" "b/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/Solution.ts" new file mode 100644 index 0000000000000..1e97dc7476727 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/Solution.ts" @@ -0,0 +1,14 @@ +function findRepeatNumber(nums: number[]): number { + let n: number = nums.length; + for (let i: number = 0; i < n; i++) { + while (nums[i] != i) { + if (nums[i] == nums[nums[i]]) return nums[i]; + swap(nums, i, nums[i]); + } + } + return -1; +}; + +function swap (nums: number[], i: number, j: number): void { + [nums[i], nums[j]] = [nums[j], nums[i]]; +} \ No newline at end of file