206. Reverse Linked List
给定一个节点连接类型,将其反向
我的代码:这种题对我来说总是要想很久思路,有的思路对但是心里很清楚肯定会超时,看了一眼讨论(没细看)才确定自己的思想,才写出来的
想思路的时候可以试着多设几个指针
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
pre = None
while head:
next ,head.next = head.next,pre
pre , head = head , next
return pre
409. Longest Palindrome
输入一个字符串,求这个字符串拆分组合成最长的回文字符串为多长
一开始思路错了,完全减去了单数个字母,其实3个a中可以贡献2个a给回文
我的代码:刚想到也可以先求和,再计算需要减的数
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: int
"""
count = collections.Counter(s)
ans = 0
flag = 0
for keys in count:
if count[keys] % 2 == 0:
ans += count[keys]
else:
ans += count[keys]-1
flag = 1
return ans + flag
大神的代码:思路一样,简写成了两行
def longestPalindrome(self, s):
"""
:type s: str
:rtype: int
"""
idic=collections.Counter(s)
return sum(idic[i] if idic[i]%2==0 else idic[i]-1 for i in idic)+any(idic[i] for i in idic if idic[i]%2!=0)
628. Maximum Product of Three Numbers
输入一个列表,求其中三个数字相乘得到的数最大值,里面会有负数,这是个坑
我的代码:
class Solution(object):
def maximumProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
return max(nums[0]*nums[1]*nums[-1],nums[-1]*nums[-2]*nums[-3])
discuss里基本都这个思路,有个用了比较少见的函数:
def maximumProduct(self, nums):
a, b = heapq.nlargest(3, nums), heapq.nsmallest(2, nums)
return max(a[0] * a[1] * a[2], b[0] * b[1] * a[0])
447. Number of Boomerangs
给出若干个坐标点,其中可能存在点(i,j,k),i到j的距离等于i到k的距离,求这个组合的个数
Example:
Input: [[0,0],[1,0],[2,0]] Output: 2 Explanation: The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]我的代码:
class Solution(object):
def numberOfBoomerangs(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
ans = 0
x,y = [m[0] for m in points],[m[1] for m in points]
for i in points:
distance = map(lambda x,y:math.sqrt((i[0]-x)**2+(i[1]-y)**2),x,y) #其实不显示距离,这里的math.sqrt可以省去
counter = collections.Counter(distance)
for k in counter.values():
ans += k*(k-1)
return ans
只看到个思路一样的代码:
res = 0
for p in points:
cmap = {}
for q in points:
f = p[0]-q[0]
s = p[1]-q[1]
cmap[f*f + s*s] = 1 + cmap.get(f*f + s*s, 0)
for k in cmap:
res += cmap[k] * (cmap[k] -1)
return res
661. Image Smoother
直接看例子吧,表达比较简单
Example 1:
Input: [[1,1,1], [1,0,1], [1,1,1]] Output: [[0, 0, 0], [0, 0, 0], [0, 0, 0]] Explanation: For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0 For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0 For the point (1,1): floor(8/9) = floor(0.88888889) = 0
我的代码:
class Solution(object):
def imageSmoother(self, M):
"""
:type M: List[List[int]]
:rtype: List[List[int]]
"""
ans = copy.deepcopy(M)
for i in range(len(M)):
for j in range(len(M[0])):
around = []
for x in [i-1,i,i+1]:
for y in [j-1,j,j+1]:
if -1 < x < len(M) and -1 < y < len(M[0]):
around.append(M[x][y])
ans[i][j] = sum(around) // len(around)
return list(ans)
花时间找了一下python列表的拷贝方式:
from:http://www.runoob.com/w3cnote/python-understanding-dict-copy-shallow-or-deep.html
以下实例是使用 copy 模块的 copy.copy( 浅拷贝 )和(copy.deepcopy ):
实例
#!/usr/bin/python
# -*-coding:utf-8 -*-
import
copy
a
=
[
1
,
2
,
3
,
4
,
[
'
a
'
,
'
b
'
]
]
#原始对象
b
=
a
#赋值,传对象的引用
c
=
copy
.
copy
(
a
)
#对象拷贝,浅拷贝
d
=
copy
.
deepcopy
(
a
)
#对象拷贝,深拷贝
a
.
append
(
5
)
#修改对象a
a
[
4
]
.
append
(
'
c
'
)
#修改对象a中的['a', 'b']数组对象
print
(
'
a =
'
,
a
)
print
(
'
b =
'
,
b
)
print
(
'
c =
'
,
c
)
print
(
'
d =
'
,
d
)
以上实例执行输出结果为:
('a = ', [1, 2, 3, 4, ['a', 'b', 'c'], 5]) ('b = ', [1, 2, 3, 4, ['a', 'b', 'c'], 5]) ('c = ', [1, 2, 3, 4, ['a', 'b', 'c']]) ('d = ', [1, 2, 3, 4, ['a', 'b']])