leetcode:Count and Say【Python版】
一次AC
字符串就是:count+char
class Solution:
# @return a string
def countAndSay(self, n):
str = ""
for i in range(n-1):
tmp = str
str = ""
c = tmp[0]
cnt = 1
for j in range(1,len(tmp)):
if tmp[j] == tmp[j-1]:
cnt += 1
else:
str += ("%d"%cnt + tmp[j-1])
cnt = 1
str += ("%d"%cnt + tmp[len(tmp)-1])
return str
leetcode:Count and Say【Python版】的更多相关文章
- 【leetcode】Min Stack -- python版
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- leetcode Count and Say python
class Solution(object): def countAndSay(self, n): """ :type n: int :rtype: str " ...
- leetcode:Path Sum【Python版】
1.类中递归调用函数需要加self # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # s ...
- leetcode:Same Tree【Python版】
1.p或q为None的情况用开始的两个if语句进行判断: 2.类中递归调用函数需要使用self进行调用: 3.代码很简洁,最后几行通过同时为None和同时非None的条件进行判断: # Definit ...
- leetcode:Single Number【Python版】
1.用双重循环逐个遍历(超时) 2.用list B的append和remove函数(超时) 3.用dict B(AC) class Solution: # @param A, a list of in ...
- leetcode:Palindrome Number【Python版】
一次AC 题目要求中有空间限制,因此没有采用字符串由量变向中间逐个对比的方法,而是采用计算翻转之后的数字与x是否相等的方法: class Solution: # @return a boolean d ...
- leetcode:Reverse Integer【Python版】
1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: cl ...
- leetcode:Symmetric Tree【Python版】
#error caused by:#1:{} 没有考虑None输入#2:{1,2,2} 没有控制h和t#3:{4,-57,-57,#,67,67,#,#,-97,-97} 没有考虑负号,将s从str变 ...
- leetcode:Valid Palindrome【Python版】
1.注意空字符串的处理: 2.注意是alphanumeric字符: 3.字符串添加字符直接用+就可以: class Solution: # @param s, a string # @return a ...
- 数据结构:顺序表(python版)
顺序表python版的实现(部分功能未实现) #!/usr/bin/env python # -*- coding:utf-8 -*- class SeqList(object): def __ini ...
随机推荐
- English trip V1 - 7.My dream car 我梦想的车 Teacher:Lamb Key: famous for
中华In this lesson you will learn to describe an object(目标). 课上内容(Lesson) famous for 以…著称,闻名 国家(名词) ...
- Ant Man CodeForces - 704B (图论,贪心)
大意: 给N个点,起点S终点T,每个点有X,A,B,C,D,根据I和J的X坐标可得I到J的距离计算公式 |xi - xj| + ci + bj seconds if j< i |xi - xj| ...
- 『科学计算』科学绘图库matplotlib练习
思想:万物皆对象 作业 第一题: import numpy as np import matplotlib.pyplot as plt x = [1, 2, 3, 1] y = [1, 3, 0, 1 ...
- Python基础--列表、元组
一.什么是列表.元组 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见 ...
- C++技能重拾
0.虽然静态成员函数不存在this指针,但还是不能在一个class里声明同名同参的虚函数和静态成员函数. 1.vftable里一个虚函数表是一个指针 2.delete本质,调用析构函数同时释放内存Ob ...
- html页面展示Json样式
一般有些做后台数据查询,要把后台返回json数据展示到页面上,如果需要展示样式更清晰.直观.一目了然,就要用到html+css+js实现这个小功能 一.css代码 pre {outline: 1px ...
- RabbitMQ特性
使用默认的exchange channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); 如果用空字符串去申明一个 ...
- forget word qz_out_b1
1★ be bi prep 使~成为: 2★ bene b əni 好,善 :祈祷 3★ bi 2, 双重, 两个 bi 4★ by b æ / 通过,在~之前
- Openwrt Support RESET Button (5)
1 Scope of Document This document describes how to support reset button under openwrt system2 Requir ...
- bzoj1621
题解: 简单判断一下怎么分 如果分的话继续递归 代码: #include<bits/stdc++.h> using namespace std; int n,k; int js(int x ...