mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-21 23:44:25 +08:00
Update solution 0541
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
@ -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)。
|
||||
|
||||
|
Reference in New Issue
Block a user