Skip to content

Commit df54373

Browse files
halfrostdezhiy
authored andcommitted
Update solution 0541
1 parent fcc3d10 commit df54373

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

note/time_complexity.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool isPrime (int n){
4949

5050
上面这段代码的时间复杂度是 O(sqrt(n)) 而不是 O(n)。
5151

52-
再举一个例子,有一个字符串数组,将数组中的每一个字符串按照字母序排序,之后再降整个字符串数组按照字典序排序。两步操作的整体时间复杂度是多少呢?
52+
再举一个例子,有一个字符串数组,将数组中的每一个字符串按照字母序排序,之后再将整个字符串数组按照字典序排序。两步操作的整体时间复杂度是多少呢?
5353

5454
如果回答是 O(n*nlog n + nlog n) = O(n^2log n),这个答案是错误的。字符串的长度和数组的长度是没有关系的,所以这两个变量应该单独计算。假设最长的字符串长度为 s,数组中有 n 个字符串。对每个字符串排序的时间复杂度是 O(slog s),将数组中每个字符串都按照字母序排序的时间复杂度是 O(n * slog s)。
5555

website/content/ChapterFour/0500~0599/0541.Reverse-String-II.md

+12
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ func reverseStr(s string, k int) string {
5353
return s
5454
}
5555

56+
func revers(s string) string {
57+
bytes := []byte(s)
58+
i, j := 0, len(bytes)-1
59+
for i < j {
60+
bytes[i], bytes[j] = bytes[j], bytes[i]
61+
i++
62+
j--
63+
}
64+
return string(bytes)
65+
}
66+
67+
5668
```
5769

5870

website/content/ChapterOne/Time_Complexity.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ bool isPrime (int n){
5353

5454
上面这段代码的时间复杂度是 O(sqrt(n)) 而不是 O(n)。
5555

56-
再举一个例子,有一个字符串数组,将数组中的每一个字符串按照字母序排序,之后再降整个字符串数组按照字典序排序。两步操作的整体时间复杂度是多少呢?
56+
再举一个例子,有一个字符串数组,将数组中的每一个字符串按照字母序排序,之后再将整个字符串数组按照字典序排序。两步操作的整体时间复杂度是多少呢?
5757

5858
如果回答是 O(n*nlog n + nlog n) = O(n^2log n),这个答案是错误的。字符串的长度和数组的长度是没有关系的,所以这两个变量应该单独计算。假设最长的字符串长度为 s,数组中有 n 个字符串。对每个字符串排序的时间复杂度是 O(slog s),将数组中每个字符串都按照字母序排序的时间复杂度是 O(n * slog s)。
5959

0 commit comments

Comments
 (0)