Skip to content

Conversation

Geekhyt
Copy link
Contributor

@Geekhyt Geekhyt commented Apr 13, 2020

No description provided.

所以返回 [0, 1]
```
## 思路
使用一层循环、每遍历到一个元素就计算该元素与target之间的差值diff,然后以diff为下标到数组temp中寻找
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 题目都加下时间复杂度分析吧。 我后面的题目也会加。 这题的复杂度不好,为O(max(nums)),建议 hashmap
  2. 可以先将一下双层循环怎么做

- 如果temp[diff]有值,则返回两个元素在数组nums的下标
- 如果没有找到,则将当前元素存入数组temp中(下标nums[i], 下标nums[i]对应的值i)

时间复杂度:O(n)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

复杂度的格式为:

复杂度分析

  • 时间复杂度:$O(N)$
  • 空间复杂度:$O(N)$

const temp = [];
for (let i = 0; i < nums.length; i++) {
const diff = target - nums[i];
if (temp[diff] != undefined) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (temp[diff] != undefined) {
if (temp[diff] != void 0) {

输出: false
```

## 思路
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

思路太简单。 更偏向关键点堆砌。建议优化为自然语言

```

## 思路
- 用哈希表存下对应的值
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这不是思路。 思路可以参考我的回溯专题


```

## 思路
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一样的问题。 这不是思路,是关键点。

并且使用递归的原因只是其比较简单,容易书写,并且数据规模合适,并不是关键


```

## 思路
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议使用自然语言描述思路。


## 关键点

- 处理有效括号
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个关键点没懂

所以你应该输出2.
```

## 思路
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议改为从小到大满足。 参考:

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        cnt = 0
        j = 0
        for i in range(len(s)):
            if j < len(g) and s[i] >= g[j]:
                cnt += 1
                j += 1
        return cnt

]
```

## 思路
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看了思路还是一头雾水。 是否可以用自然语言描述下

- 最容易想到的就是暴力枚举,我们可以利用两层 for 循环来遍历每个元素,并查找满足条件的目标元素。
- 不过这样时间复杂度为 O(N^2),空间复杂度为 O(1),时间复杂度较高,我们要想办法进行优化。
- 我们增加一个 Map 记录已经遍历过的数字及其对应的索引值。
- 这样当遍历一个新数字的时候去 Map 里查询,与该数的差值是否已经在前面的数字中出现过
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 这样当遍历一个新数字的时候去 Map 里查询,与该数的差值是否已经在前面的数字中出现过
- 这样当遍历一个新数字的时候去 Map 里查询,target 与该数的差值是否已经在前面的数字中出现过

@@ -15,16 +15,22 @@ https://leetcode-cn.com/problems/two-sum
所以返回 [0, 1]
```
## 思路
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不建议无序列表

## 思路
- 用哈希表存下对应的值
- 递归
## 思想
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## 思想
## 思路


## 关键点
利用回溯思想解题,在for循环中调用递归。
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
利用回溯思想解题,在for循环中调用递归。
- 回溯


## 关键点

对位运算公式的理解和运用
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
对位运算公式的理解和运用
- 位运算
- DFS(深度优先搜索)

@azl397985856 azl397985856 changed the title 修改错别字,添加题解 feat: 修改错别字,并添加题解 Apr 15, 2020
@azl397985856 azl397985856 merged commit f04b4f9 into azl397985856:master Apr 15, 2020
@azl397985856 azl397985856 added enhancement New feature or request bug Something isn't working labels Apr 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants