update 0383.赎金信:添加复杂度分析

This commit is contained in:
Yuhao Ju
2023-03-19 11:16:15 +08:00
committed by GitHub
parent 08a984d61c
commit c91ee8fdc2

View File

@ -39,8 +39,6 @@ canConstruct("aa", "aab") -> true
那么第一个思路其实就是暴力枚举了两层for循环不断去寻找代码如下
```CPP
// 时间复杂度: O(n^2)
// 空间复杂度O(1)
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
@ -62,6 +60,9 @@ public:
};
```
* 时间复杂度: O(n^2)
* 空间复杂度: O(1)
这里时间复杂度是比较高的而且里面还有一个字符串删除也就是erase的操作也是费时的当然这段代码也可以过这道题。
@ -78,8 +79,6 @@ public:
代码如下:
```CPP
// 时间复杂度: O(n)
// 空间复杂度O(1)
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
@ -105,6 +104,10 @@ public:
};
```
* 时间复杂度: O(n)
* 空间复杂度: O(1)
## 其他语言版本