单例模式,用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. UIView之setNeedsDisplay与drawRect 和 setNeedsLayout与layoutSubViews

    1.UIView的setNeedsDisplay和setNeedsLayout方法 首先两个方法都是异步执行的.而setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到  UI ...

  2. 打扮IDEA更换主题

    原文链接:https://blog.csdn.net/github_39577257/article/details/80629750 当我们安装一个新的IDEA工具时,第一次进入时会提示我们选择一个 ...

  3. scrollto 到指定位置

    goTo = function(target){ var scrollT = document.body.scrollTop|| document.documentElement.scrollTop ...

  4. json "key" 注意

    json 的key只能是字符串 必须使用 双引号

  5. CSS 加号选择器("+")

    加号选择器("+"):就是指对找到的某类的元素除第一个元素以外的兄弟元素起作用,即第一个元素不起作用,后面的兄弟元素都会起作用   效果:给每一个li加一个border-left, ...

  6. 51 Nod 1050 dp

    1050 循环数组最大子段和 1 秒 131,072 KB 10 分 2 级题   N个整数组成的循环序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的连 ...

  7. python之类与对象(1)

    面向对象编程是最有效的软件编写方法之一.编写类时,定义一群对象都有的通用行为.基于类创建对象时,每个对象都自动具备这种通用行为,然后可以根据需要赋予每个对象的独特的个性. 1. 类与对象的语法规范 关 ...

  8. 02-线性结构3 Reversing Linked List (25 分)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  9. apt 命令大全

    #1. 搜索包 sudo apt-cache search package #2.获取包的相关信息,如说明,大小,版本. sudo apt-cache show package #3.了解包的依赖 s ...

  10. Vue.js 使用注意事项

    Vue.js 使用注意事项 1 过滤器主要用于简单的文本转换,如果要实现复杂的数据变换,应使用计算属性 指令的使用 v-bind基本用于HTML元素上的属性,如id.class.href.src等 v ...