mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #302 from z80160280/master
添加 0024 0242 0202 python版本
This commit is contained in:
@ -129,6 +129,23 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
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:
|
||||||
```go
|
```go
|
||||||
|
@ -108,7 +108,29 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
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:
|
Go:
|
||||||
```go
|
```go
|
||||||
|
@ -113,7 +113,22 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
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:
|
||||||
```go
|
```go
|
||||||
|
Reference in New Issue
Block a user