Leetcode 算法题09
来源:互联网 发布:九州量子 网络揭牌 编辑:程序博客网 时间:2024/05/16 17:18
206. Reverse Linked List
给定一个节点连接类型,将其反向
我的代码:这种题对我来说总是要想很久思路,有的思路对但是心里很清楚肯定会超时,看了一眼讨论(没细看)才确定自己的思想,才写出来的
想思路的时候可以试着多设几个指针
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass 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:2Explanation: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) = 0For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0For 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 -*-
importcopy
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']])
阅读全文
0 0
- Leetcode 算法题09
- Leetcode sql算法题
- leetcode算法刷题
- LeetCode | HouseRobber 算法题
- leetcode算法第二题
- LeetCode算法题典
- leetcode算法刷题
- HappyNumber leetcode算法题
- Leetcode 算法题01
- Leetcode 算法题02
- Leetcode 算法题03
- Leetcode 算法题04
- Leetcode 算法题05
- Leetcode 算法题06
- Leetcode 算法题07
- Leetcode 算法题08
- LeetCode算法第三题
- Leetcode 算法题10
- The application may be doing too much work on its main thread.
- 蓝牙枪
- Android RxJava 实战系列:优雅实现 网络请求嵌套回调
- java使用log4j进行日志记录
- MATLAB-文本数据读取
- Leetcode 算法题09
- CSDN-markdown编辑器模板
- 操作系统原理
- Java开发手册(一):环境准备
- assignment3
- 给定一个数字字符串,返回数字可能表示的所有可能的字母组合。数字与字母的对应关系和手机按键对应。
- 获取轮廓深度
- 这样的创新招聘会你看过吗?这样的海航好不好呢?
- 确保对象的唯一性——单例模式 (五)