Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions problems/0024.两两交换链表中的节点.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,23 @@ class Solution {
```

Python:
```python
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
dummy = ListNode(0) #设置一个虚拟头结点
dummy.next = head
cur = dummy
while cur.next and cur.next.next:
tmp = cur.next #记录临时节点
tmp1 = cur.next.next.next #记录临时节点

cur.next = cur.next.next #步骤一
cur.next.next = tmp #步骤二
cur.next.next.next = tmp1 #步骤三

cur = cur.next.next #cur移动两位,准备下一轮交换
return dummy.next
```

Go:
```go
Expand Down
24 changes: 23 additions & 1 deletion problems/0202.快乐数.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,29 @@ class Solution {
```

Python:

```python
class Solution:
def isHappy(self, n: int) -> bool:
set_ = set()
while 1:
sum_ = self.getSum(n)
if sum_ == 1:
return True
#如果这个sum曾经出现过,说明已经陷入了无限循环了,立刻return false
if sum_ in set_:
return False
else:
set_.add(sum_)
n = sum_

#取数值各个位上的单数之和
def getSum(self, n):
sum_ = 0
while n > 0:
sum_ += (n%10) * (n%10)
n //= 10
return sum_
```

Go:

Expand Down
17 changes: 16 additions & 1 deletion problems/0242.有效的字母异位词.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,22 @@ class Solution {
```

Python:

```python
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
record = [0] * 26
for i in range(len(s)):
#并不需要记住字符a的ASCII,只要求出一个相对数值就可以了
record[ord(s[i]) - ord("a")] += 1
print(record)
for i in range(len(t)):
record[ord(t[i]) - ord("a")] -= 1
for i in range(26):
if record[i] != 0:
#record数组如果有的元素不为零0,说明字符串s和t 一定是谁多了字符或者谁少了字符。
return False
return True
```

Go:
```go
Expand Down