leetcode:Count and Say【Python版】】的更多相关文章

题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.getMin() -- Retrieve the minimum…
class Solution(object): def countAndSay(self, n): """ :type n: int :rtype: str """ s=' for i in range(2,n+1): s=self.count(s) return s def count(self,s): t='';count=0;curr='#' for i in s: if i != curr: if curr != '#': t+=str(…
1.类中递归调用函数需要加self # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # @param root, a tree node # @param sum, an integer # @return a boolean def hasPath…
1.p或q为None的情况用开始的两个if语句进行判断: 2.类中递归调用函数需要使用self进行调用: 3.代码很简洁,最后几行通过同时为None和同时非None的条件进行判断: # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # @param p…
1.用双重循环逐个遍历(超时) 2.用list B的append和remove函数(超时) 3.用dict B(AC) class Solution: # @param A, a list of integer # @return an integer def singleNumber(self, A): B = {} for i in A: if i not in B: B[i] = 1 else: B[i] = 2 for i in B: if B[i] == 1: ret = i brea…
一次AC 题目要求中有空间限制,因此没有采用字符串由量变向中间逐个对比的方法,而是采用计算翻转之后的数字与x是否相等的方法: class Solution: # @return a boolean def isPalindrome(self, x): o = x ret = 0 flag = 1 if x < 0: return False while(x!=0): ret = ret*10+x%10 x = x/10 return ret == o…
1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: class Solution: # @return an integer def reverse(self, x): ret = 0 flag = 1 if x < 0: flag = -1 x *= -1 while(x!=0): ret = ret*10+x%10 x = x/10 return r…
#error caused by:#1:{} 没有考虑None输入#2:{1,2,2} 没有控制h和t#3:{4,-57,-57,#,67,67,#,#,-97,-97} 没有考虑负号,将s从str变成list,采用9999代表空数值: --------------------- 逐层进行对称性验证,出现不对称就结束: # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val…
1.注意空字符串的处理: 2.注意是alphanumeric字符: 3.字符串添加字符直接用+就可以: class Solution: # @param s, a string # @return a boolean def isPalindrome(self, s): ret = False s = s.lower() ss = "" for i in s: if i.isalnum(): ss += i h = 0 e = len(ss)-1 while(h<e): if(s…
顺序表python版的实现(部分功能未实现) #!/usr/bin/env python # -*- coding:utf-8 -*- class SeqList(object): def __init__(self, max=8): self.max = max #创建默认为8 self.num = 0 self.date = [None] * self.max #list()会默认创建八个元素大小的列表,num=0,并有链接关系 #用list实现list有些荒谬,全当练习 #self.las…