diff --git a/note/time_complexity.md b/note/time_complexity.md index 8e47b6a4..237e34f1 100644 --- a/note/time_complexity.md +++ b/note/time_complexity.md @@ -49,7 +49,7 @@ bool isPrime (int n){ 上面这段代码的时间复杂度是 O(sqrt(n)) 而不是 O(n)。 -再举一个例子,有一个字符串数组,将数组中的每一个字符串按照字母序排序,之后再降整个字符串数组按照字典序排序。两步操作的整体时间复杂度是多少呢? +再举一个例子,有一个字符串数组,将数组中的每一个字符串按照字母序排序,之后再将整个字符串数组按照字典序排序。两步操作的整体时间复杂度是多少呢? 如果回答是 O(n*nlog n + nlog n) = O(n^2log n),这个答案是错误的。字符串的长度和数组的长度是没有关系的,所以这两个变量应该单独计算。假设最长的字符串长度为 s,数组中有 n 个字符串。对每个字符串排序的时间复杂度是 O(slog s),将数组中每个字符串都按照字母序排序的时间复杂度是 O(n * slog s)。 diff --git a/website/content/ChapterFour/0500~0599/0541.Reverse-String-II.md b/website/content/ChapterFour/0500~0599/0541.Reverse-String-II.md index 48133a06..72c482eb 100755 --- a/website/content/ChapterFour/0500~0599/0541.Reverse-String-II.md +++ b/website/content/ChapterFour/0500~0599/0541.Reverse-String-II.md @@ -53,6 +53,18 @@ func reverseStr(s string, k int) string { return s } +func revers(s string) string { + bytes := []byte(s) + i, j := 0, len(bytes)-1 + for i < j { + bytes[i], bytes[j] = bytes[j], bytes[i] + i++ + j-- + } + return string(bytes) +} + + ``` diff --git a/website/content/ChapterOne/Time_Complexity.md b/website/content/ChapterOne/Time_Complexity.md index 0d4630ec..647e0a9e 100644 --- a/website/content/ChapterOne/Time_Complexity.md +++ b/website/content/ChapterOne/Time_Complexity.md @@ -53,7 +53,7 @@ bool isPrime (int n){ 上面这段代码的时间复杂度是 O(sqrt(n)) 而不是 O(n)。 -再举一个例子,有一个字符串数组,将数组中的每一个字符串按照字母序排序,之后再降整个字符串数组按照字典序排序。两步操作的整体时间复杂度是多少呢? +再举一个例子,有一个字符串数组,将数组中的每一个字符串按照字母序排序,之后再将整个字符串数组按照字典序排序。两步操作的整体时间复杂度是多少呢? 如果回答是 O(n*nlog n + nlog n) = O(n^2log n),这个答案是错误的。字符串的长度和数组的长度是没有关系的,所以这两个变量应该单独计算。假设最长的字符串长度为 s,数组中有 n 个字符串。对每个字符串排序的时间复杂度是 O(slog s),将数组中每个字符串都按照字母序排序的时间复杂度是 O(n * slog s)。