美文网首页程序员生活不易 我用python
【第五章】python算法刷题开始(1)

【第五章】python算法刷题开始(1)

作者: 你好夜故事 | 来源:发表于2018-08-01 10:56 被阅读1次

有了python基础知识积累,决定再花些时间巩固一下,这样在后期项目开发中会更加轻松,废话不多说,是用的刷题网站是lintCode,直接看题:

1、A+B问题:

A+B问题

非标准型答案:

class Solution:
    """
    @param a: An integer
    @param b: An integer
    @return: The sum of a and b 
    """
    def aplusb(self, a, b):
        return a+b

2、尾部的零

尾部的零

非标准型答案:

class Solution:
    """
    @param: n: An integer
    @return: An integer, denote the number of trailing zeros in n!
    """
    def trailingZeros(self,n):
        count = 0
        sum=n
        sign=1
        while n>1:
            sum=sum*(n-1)
            n=n-1
        sumStr = str(sum);
        length = len(sumStr)-1
        while length>=0 and sign==1:
            if int(sumStr[length])==0:
                count+=1
            else:
                sign=0
            length-=1
        return count

答案分析:上面的程序在lintCode上面,效率是不过关的,后期进行优化

3、统计数字

统计数字
class Solution:
    """
    @param k: An integer
    @param n: An integer
    @return: An integer denote the count of digit k in 1..n
    """
    def digitCounts(self, k, n):
        i = 0
        count = 0
        while i<=n:
            tempStr = str(i);
            j=0
            while j<len(tempStr):
                if(int(tempStr[j])==k):
                    count+=1
                j+=1
            i+=1
        return count

今天就只刷了三道题,大家也可以使用编辑器自行敲写一遍,加深印象,后续还会继续发布最新刷题记录。

相关文章

  • 【第五章】python算法刷题开始(1)

    有了python基础知识积累,决定再花些时间巩固一下,这样在后期项目开发中会更加轻松,废话不多说,是用的刷题网站是...

  • 零基础python刷leetcode -- 1. Two Sum

    算法很重要,但是每天也需要学学python,于是就想用python刷leetcode 的算法题,从第一题开始,从简...

  • 零基础python刷leetcode -- 3. Longest

    算法很重要,但是每天也需要学学python,于是就想用python刷leetcode 的算法题,和我一起开始零基础...

  • 零基础python刷leetcode -- 2. Add Two

    算法很重要,但是每天也需要学学python,于是就想用python刷leetcode 的算法题,和我一起开始零基础...

  • python leetcode 628

    这是用python刷的第一道算法题。原题:Given an integer array, find three n...

  • lettcode刷题之贪心

    leetcode刷题,使用python 1, 跳跃游戏 II —— 0045 贪心算法给定一个长度为 n 的 0...

  • 剑指offer-Python版(上)

    剑指offer上面的66道算法题是面试高频题,书中用C/C++写的答案,本篇笔记用python刷一遍所有的算法题,...

  • TwoSum

    刷题当然要从TwoSum开始了~~python刷题果然容易~~~class Solution(object):de...

  • 为什么坚持写字?

    周六晚上,静静的写点文字,来完成今天的日更吧,其实今天也有刷剑桥offer的算法题,自从开始刷算法题,每天的...

  • 分治算法 -Python刷题笔记

    分而治之分治算法 Divide and Conquer就是把复杂问题分解成大小合适的子问题然后求解,最后把子问题解...

网友评论

    本文标题:【第五章】python算法刷题开始(1)

    本文链接:https://www.haomeiwen.com/subject/efulvftx.html