leetcode Count and Say python】的更多相关文章

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(…
LeetCode初级算法的Python实现--排序和搜索.设计问题.数学及其他 1.排序和搜索 class Solution(object): # 合并两个有序数组 def merge(self, nums1, m, nums2, n): """ :type nums1: List[int] :type m: int :type nums2: List[int] :type n: int :rtype: void Do not return anything, modify…
LeetCode初级算法的Python实现--字符串 # 反转字符串 def reverseString(s): return s[::-1] # 颠倒数字 def reverse(x): if x < 0: flag = -2 ** 31 result = -1 * int(str(x)[1:][::-1]) if result < flag: return 0 else: return result else: flag = 2 ** 31 - 1 result = int(str(x)[…
LeetCode初级算法的Python实现--数组 # -*- coding: utf-8 -*- """ @Created on 2018/6/3 17:06 @author: ZhifengFang """ # 排列数组删除重复项 def removeDuplicates(nums): if len(nums) <= 1: return len(nums) i = 1 while len(nums) != i: if nums[i] =…
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://blog.csdn.net/doc_sgl/article/details/13341405   http://chaoren.is-programmer.com/ 题意:给定start单词,end单词,以及一个dict字典.要求找出start到end的所有最短路径,路径上的每个单词都要出现在dict…
LeetCode初级算法的Python实现--链表 之前没有接触过Python编写的链表,所以这里记录一下思路.这里前面的代码是和leetcode中的一样,因为做题需要调用,所以下面会给出. 首先定义链表的节点类. # 链表节点 class ListNode(object): def __init__(self, x): self.val = x # 节点值 self.next = None 其次分别定义将列表转换成链表和将链表转换成字符串的函数: # 将列表转换成链表 def stringTo…
文章目录 Hash表简介 基本思想 建立步骤 问题 Hash表实现 Hash函数构造 冲突处理方法 leetcode两数之和python实现 题目描述 基于Hash思想的实现 Hash表简介 基本思想 哈希存储的基本思想是根据当前待存储数据的特征,以记录关键字(key)为自变量,设计一个哈希函数Hash,根据Hash计算出对应的函数值Hash(key),以这个值(哈希地址)作为数据元素的地址,并将数据元素存入到相应地址的存储单元中.按照这个思想构造的表就叫做哈希表(Hash table,也叫散列…
一次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"%c…
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99]) Hint: A direct…
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j (i ≤ j), inclusive. Note:A naive algorithm of O(n2) is trivial.…