单例模式,用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 刷题记录··的更多相关文章

  1. PE刷题记录

    PE刷题记录 PE60 / 20%dif 这道题比较坑爹. 所有可以相连的素数可以构成一张图,建出这张图,在其中找它的大小为5的团.注意上界的估算,大概在1W以内.1W内有1229个素数,处理出这些素 ...

  2. lintcode 刷题 by python 总结(1)

    博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...

  3. leetcode刷题记录--js

    leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

  4. Leetcode刷题记录(python3)

    Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...

  5. 刷题记录:[HarekazeCTF2019]encode_and_encode

    目录 刷题记录:[HarekazeCTF2019]encode_and_encode 一.知识点 JSON转义字符绕过 php伪协议 刷题记录:[HarekazeCTF2019]encode_and_ ...

  6. 刷题记录:[De1CTF 2019]Giftbox && Comment

    目录 刷题记录:[De1CTF 2019]Giftbox && Comment 一.知识点 1.sql注入 && totp 2.RCE 3.源码泄露 4.敏感文件读取 ...

  7. 刷题记录:[强网杯 2019]Upload

    目录 刷题记录:[强网杯 2019]Upload 一.知识点 1.源码泄露 2.php反序列化 刷题记录:[强网杯 2019]Upload 题目复现链接:https://buuoj.cn/challe ...

  8. 刷题记录:[XNUCA2019Qualifier]EasyPHP

    目录 刷题记录:[XNUCA2019Qualifier]EasyPHP 解法一 1.error_log结合log_errors自定义错误日志 2.include_path设置包含路径 3.php_va ...

  9. 刷题记录:[DDCTF 2019]homebrew event loop

    目录 刷题记录:[DDCTF 2019]homebrew event loop 知识点 1.逻辑漏洞 2.flask session解密 总结 刷题记录:[DDCTF 2019]homebrew ev ...

随机推荐

  1. 题目1022:游船出租(hash简单应用)

    问题来源 http://ac.jobdu.com/problem.php?pid=1022 问题描述 每次输入:船号(1~100) 键值(S或E) 发生时间(小时:分钟).当船号为0时,代表一天结束: ...

  2. linux I/O函数使用

    一.lseek lseek函数的作用是用来重新定位文件读写的位移. 头文件以及函数声明 #include <sys/types.h> #include <unistd.h> o ...

  3. 4G和有线网络的自动切换

    最近项目有个需求,把移动服务器设备(Ubuntu14.04)安装4G模块,但如果连接有线时,可以自动切换到有线,以降低移动流量带来的费用. 以下是我实现的方法(经过一番痛苦的摸索) 1. 脚本/opt ...

  4. 关于dedecms数据量大以后生成目录缓慢的问题解决

    四月份的时候博客被封.我不知情.因为一直很忙,没有来得及看.前两天来看以后,发现居然被封,吓傻了我. 赶紧找原因,原来是转载了某个人的博文,被他举报了,然后就被封了. 觉得很伤心,毕竟这个博客陪伴了我 ...

  5. Linux下配置redis,c#简单调用

    redis比较流行的nosql库: 我这里测试本机window系统,虚拟机安装linux系统,linux系统部署redis,windwo系统,c#调用linux系统的redis 第一步:linux下安 ...

  6. Form Authentication

    1.创建登陆的控制器和视图,实现登陆基本功能 2.创建视图模型,并在Action里面引用. 3.创建一个接口两个类,那个IUserPricipal接口要实现IPrincipal接口,UserPrici ...

  7. HTTP请求的常用方法有哪些

    HTTP请求的常用方法有:GET方法.POST方法.HEAD方法.PUT方法.DELETE方法.CONNECT方法.OPTIONS方法.TRACE方法.下面本篇文章就给大家介绍具体介绍一下HTTP请求 ...

  8. linux新增特性timerfd

    https://blog.csdn.net/shreck66/article/details/49745149

  9. java实现猴子选大王问题(约瑟夫问题)

    题目:m只猴子围成一圈报数,报n的猴子自动离开,然后下一位重新从1开始报数,一直循环,最后剩下的那个猴子就是猴大王,写出程序求出最后是大王的那只猴子最初的位置. package learn; impo ...

  10. SpagoBI系列----------[01]SpagoBI简介及安装步骤

    商务智能套件SpagoBI提供一个基于J2EE的框架用于管理BI对象如报表.OLAP分析.仪表盘.记分卡以及数据挖掘模型等的开源BI产品.它提供的BI管理器能 够控制.校验.验证与分发这些BI对象. ...