From c027a13f6ed82fd6150807304c57d52972e32f94 Mon Sep 17 00:00:00 2001 From: yi-ge Date: Fri, 8 May 2020 13:12:12 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E5=B7=A6=E6=97=8B=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 9 ++++++++- README.md | 4 ++++ src/string/zuo-xuan-zhuan-zi-fu-chuan-lcof.js | 8 ++++++++ test/string/zuo-xuan-zhuan-zi-fu-chuan-lcof.test.js | 6 ++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/string/zuo-xuan-zhuan-zi-fu-chuan-lcof.js create mode 100644 test/string/zuo-xuan-zhuan-zi-fu-chuan-lcof.test.js diff --git a/.vscode/settings.json b/.vscode/settings.json index 29cc9c7..7d8629b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,11 +1,18 @@ { "cSpell.words": [ + "abcdefg", + "cdefgab", + "chuan", "dvdf", "lcci", "lcof", + "lrloseumgh", "mincost", "nums", "pwwkew", - "zhong" + "umghlrlose", + "xuan", + "zhong", + "zhuan" ] } \ No newline at end of file diff --git a/README.md b/README.md index d62267f..2a0721b 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - 面试题参考思路,不严谨实现 廖雪峰 不要使用JavaScript内置的parseInt()函数,利用map和reduce操作实现一个string2int()函数。 https://www.liaoxuefeng.com/wiki/1022910821149312/1024322552460832 +- [左旋转字符串](src/string/zuo-xuan-zhuan-zi-fu-chuan-lcof.js) + + - LeetCode 面试题58 - II. 左旋转字符串 https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/ + ## 数组 - [电话号码的字母组合](src/array/letter-combinations-of-a-phone-number.js) diff --git a/src/string/zuo-xuan-zhuan-zi-fu-chuan-lcof.js b/src/string/zuo-xuan-zhuan-zi-fu-chuan-lcof.js new file mode 100644 index 0000000..de51a37 --- /dev/null +++ b/src/string/zuo-xuan-zhuan-zi-fu-chuan-lcof.js @@ -0,0 +1,8 @@ +/** + * @param {string} s + * @param {number} n + * @return {string} + */ +export const reverseLeftWords = function (s, n) { + return (s + s).substr(n, s.length) +} diff --git a/test/string/zuo-xuan-zhuan-zi-fu-chuan-lcof.test.js b/test/string/zuo-xuan-zhuan-zi-fu-chuan-lcof.test.js new file mode 100644 index 0000000..43c52de --- /dev/null +++ b/test/string/zuo-xuan-zhuan-zi-fu-chuan-lcof.test.js @@ -0,0 +1,6 @@ +import { reverseLeftWords } from '../../src/string/zuo-xuan-zhuan-zi-fu-chuan-lcof' + +test('左旋转字符串', () => { + expect(reverseLeftWords('abcdefg', 2)).toBe('cdefgab') + expect(reverseLeftWords('lrloseumgh', 6)).toBe('umghlrlose') +})