lintcode 刷题记录··
单例模式,用C#实现过单例模式,python区别就是类里边的静态方法写法不一样,python叫类方法,函数之前加@classmethod
class Solution:
# @return: The same instance of this class every time
__m_Solution=None @classmethod
def getInstance(self):
# write your code here
if self.__m_Solution == None:
self.__m_Solution=Solution()
return self.__m_Solution def __init__(self):
pass
遍历二叉树路径,输出与给定值相同的所有路径,这里是递归调用的,本来像把递归改成循环的 但是高了好久 没搞出来····
class Solution:
# @param {int} target an integer
# @return {int[][]} all valid paths
def binaryTreePathSum(self, root, target):
# Write your code here
res = []
path = []
import copy
def getallpath(current, path, res):
if current == None: return
path.append(current.val)
if current.left != None:
getallpath(current.left,copy.deepcopy(path), res)
if current.right != None:
getallpath(current.right,copy.deepcopy(path),res)
if current.left is None and current.right is None:
res.append(path) getallpath(root,path,res)
ps=[]
for p in res:
sum=0
for v in p:
sum=sum+v
if sum==target:ps.append(p)
return ps
三角形计数 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形
直接遍历会提示超过时间限制
选择排序加二分查找的方法来提高效率,python默认的递归有上线,需要设置, 还有二分查找的方法是先确认最大最小边然后找第三边,这样条件就变成一个
class Solution:
# @param S: a list of integers
# @return: a integer
def triangleCount(self, S):
# write your code here
def quicksort(a): # 快速排序
if len(a) < 2:
return a
else:
pivot = a[0]
less = [i for i in a[1:] if i <= pivot]
greator = [i for i in a[1:] if i > pivot]
return quicksort(less) + [pivot] + quicksort(greator) n = len(S)
if n < 3: return
import sys
sys.setrecursionlimit(1000000)
S = quicksort(S)
sum = 0
for i in range(n):
for j in range(i + 1, n):
l = i + 1
r = j
target = S[j] - S[i]
while l < r:
mid = (l + r) // 2
if S[mid] > target:
r = mid
else:
l = mid + 1
sum = sum + j - l return sum
将一个二叉查找树按照中序遍历转换成双向链表。
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
this.val = val
this.left, this.right = None, None
Definition of Doubly-ListNode
class DoublyListNode(object): def __init__(self, val, next=None):
self.val = val
self.next = self.prev = next
""" class Solution:
"""
@param root, the root of tree
@return: a doubly list node
"""
def bstToDoublyList(self, root):
# Write your code here
l=[]
if root is None:return
stack=[]
current=root
dlist=None
cur=None
last=None
while len(stack)> 0 or current is not None:
while current is not None:
stack.append(current)
current=current.left
current=stack.pop()
l.append(current.val)
cur=DoublyListNode(current.val)
cur.prev=last
if last is not None: last.next=cur
last=cur
if dlist is None :dlist=cur
current=current.right
return dlist
lintcode 刷题记录··的更多相关文章
- PE刷题记录
PE刷题记录 PE60 / 20%dif 这道题比较坑爹. 所有可以相连的素数可以构成一张图,建出这张图,在其中找它的大小为5的团.注意上界的估算,大概在1W以内.1W内有1229个素数,处理出这些素 ...
- lintcode 刷题 by python 总结(1)
博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- Leetcode刷题记录(python3)
Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...
- 刷题记录:[HarekazeCTF2019]encode_and_encode
目录 刷题记录:[HarekazeCTF2019]encode_and_encode 一.知识点 JSON转义字符绕过 php伪协议 刷题记录:[HarekazeCTF2019]encode_and_ ...
- 刷题记录:[De1CTF 2019]Giftbox && Comment
目录 刷题记录:[De1CTF 2019]Giftbox && Comment 一.知识点 1.sql注入 && totp 2.RCE 3.源码泄露 4.敏感文件读取 ...
- 刷题记录:[强网杯 2019]Upload
目录 刷题记录:[强网杯 2019]Upload 一.知识点 1.源码泄露 2.php反序列化 刷题记录:[强网杯 2019]Upload 题目复现链接:https://buuoj.cn/challe ...
- 刷题记录:[XNUCA2019Qualifier]EasyPHP
目录 刷题记录:[XNUCA2019Qualifier]EasyPHP 解法一 1.error_log结合log_errors自定义错误日志 2.include_path设置包含路径 3.php_va ...
- 刷题记录:[DDCTF 2019]homebrew event loop
目录 刷题记录:[DDCTF 2019]homebrew event loop 知识点 1.逻辑漏洞 2.flask session解密 总结 刷题记录:[DDCTF 2019]homebrew ev ...
随机推荐
- c语言-勒让德多项式求解+时间测定
#include<iostream>#include<ctime>#include<cstdlib>using namespace std; float LRD(i ...
- vue关闭令人抓狂的ESlint 语法检测配置方法
随便改个vue 一堆报错 其实我并不反对这些语法检测,但是像许多反个人意愿的那就真的不得不吐槽了,比如vue-cli脚手架创建的默认eslint规则: 代码末尾不能加分号 ; 代码中不能存在多行空行 ...
- origin显示三维曲面
准备数据并选中数据: 这里如果只关心z<1部分的趋势,可以对Z轴范围进行调整,双击Z轴的数字: 然后修改显色条的范围,双击曲面: 最后让曲面最上面部分clip掉: 成功了:
- 洛谷 P2053 [SCOI2007]修车(最小费用最大流)
题解 最小费用最大流 n和m是反着的 首先, \[ ans = \sum{cost[i][j]}*k \] 其中,\(k\)为它在当前技术人员那里,排倒数第\(k\)个修 我们可以对于每个技术人员进行 ...
- Python学习 day13
一.可迭代对象和迭代器 1.回顾可以被for循环的对象 list.dic.str.set.tuple.文件句柄f.range().enumerate() 只有可迭代对象才能被for循环,当我们遇到一个 ...
- CSAPP阅读笔记-汇编语言初探(控制类指令)-来自第三章3.6的笔记-P135-P163
1.正溢出与负溢出: 首先,一个正数与一个负数相加,不可能溢出,因为结果的绝对值一定小于两个加数的绝对值,既然两个加数能合理表示出来,结果一定也能合理表示出来. 其次,正溢出是由于两个很大的正数相加, ...
- redux-thunk, redux-logger 阮一峰 ( react中间件 )
http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_two_async_operations.html Redux 入门教程(二):中 ...
- (转)CentOS7 LVM添加硬盘及扩容
原文:http://blog.51cto.com/qicheng0211/1620171 9818人阅读 一.LVM简介 LVM是 Logical Volume Manager(逻辑卷管理)的简写 ...
- ios UITableView 异步加载图片并防止错位
UITableView 重用 UITableViewCell 并异步加载图片时会出现图片错乱的情况 对错位原因不明白的同学请参考我的另外一篇随笔:http://www.cnblogs.com/lesl ...
- sencha touch SortableList 的使用
转: sencha touch 2.3 多了一个 SortableList plugin, 可实现 list item 的拖动交换 Ext.require('Ext.plugin.SortableL ...