0
点赞
收藏
分享

微信扫一扫

每日一练 — 2022.02.19

南柯Taylor 2022-02-19 阅读 54

文章目录


一,路径交叉

1,程序简介

给你一个整数数组 distance 。

从 X-Y 平面上的点 (0,0) 开始,先向北移动 distance[0] 米,然后向西移动 distance[1] 米,向南移动 distance[2] 米,向东移动 distance[3] 米,持续移动。也就是说,每次移动后你的方位会发生逆时针变化。

判断你所经过的路径是否相交。如果相交,返回 true ;否则,返回 false 。

示例 1:

在这里插入图片描述

  • 输入:distance = [2,1,1,2]
  • 输出:true

示例 2:

在这里插入图片描述

  • 输入:distance = [1,2,3,4]
  • 输出:false

示例 3:

在这里插入图片描述

  • 输入:distance = [1,1,1,1]
  • 输出:true

提示:

  • 1 < = d i s t a n c e . l e n g t h < = 1 0 5 1 <= distance.length <= 10^5 1<=distance.length<=105
  • 1 < = d i s t a n c e [ i ] < = 1 0 5 1 <= distance[i] <= 10^5 1<=distance[i]<=105

2,程序代码

# -*- coding: utf-8 -*-
"""
Created on Sat Feb 19 06:23:44 2022
Function: 路径交叉
@author: 小梁aixj
"""
class Solution:
    def isSelfCrossing(self, x: List[int]) -> bool:
        n = len(x)
        if n < 3: return False
        for i in range(3, n):
            if x[i] >= x[i - 2] and x[i - 3] >= x[i - 1]: 
                return True
            if i >= 4 and x[i - 3] == x[i - 1] and x[i] + x[i - 4] >= x[i - 2]: 
                return True
            if i >= 5 and x[i - 3] >= x[i - 1] and x[i - 2] >= x[i - 4] and x[i - 1] + x[i - 5] >= x[i - 3] and x[i] + x[i - 4] >= x[i - 2]: 
                return True
        return False

二,阶乘后的零

1,程序简介

给定一个整数 n ,返回 n! 结果中尾随零的数量。

提示 n! = n * (n - 1) * (n - 2) * … * 3 * 2 * 1

示例 1:

  • 输入:n = 3
  • 输出:0
  • 解释:3! = 6 ,不含尾随 0

示例 2:

  • 输入:n = 5
  • 输出:1
  • 解释:5! = 120 ,有一个尾随 0

示例 3:

  • 输入:n = 0
  • 输出:0

提示:

  • 0 < = n < = 1 0 4 0 <= n <= 10^4 0<=n<=104

进阶:

  • 你可以设计并实现对数时间复杂度的算法来解决此问题吗?

2,程序代码

# -*- coding: utf-8 -*-
"""
Created on Sat Feb 19 06:25:53 2022
Function: 阶乘后的零
@author: 小梁aixj
"""
class Solution:
    def trailingZeroes(self, n: int) -> int:
        zero_count = 0
        current_multiple = 5
        while n >= current_multiple:
            zero_count += n // current_multiple
            current_multiple *= 5
        return zero_count

三,整数转换英文表示

1,程序简介

将非负整数 num 转换为其对应的英文表示。

示例 1:

  • 输入:num = 123
  • 输出:“One Hundred Twenty Three”

示例 2:

  • 输入:num = 12345
  • 输出:“Twelve Thousand Three Hundred Forty Five”

示例 3:

  • 输入:num = 1234567
  • 输出:“One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven”

示例 4:

  • 输入:num = 1234567891
  • 输出:“One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One”

提示:

  • 0 < = n u m < = 2 31 − 1 0 <= num <= 2^{31} - 1 0<=num<=2311

2,程序代码

# -*- coding: utf-8 -*-
"""
Created on Sat Feb 19 06:38:47 2022
Function: 整数转换英文表示
@author: 小梁aixj
"""
class Solution(object):
    def numberToWords(self, num):
        """
        :type num: int
        :rtype: str
        """
        def helper(num): 
            n = int(num)
            num = str(n)
            if n < 100:
                return subhelper(num)
            else:
                return ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"][int(num[0]) - 1] + " Hundred " + subhelper(num[1:]) if num[1:] != "00" else ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"][int(num[0]) - 1] + " Hundred"
        def subhelper(num): 
            n = int(num)
            l1 = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
            l2 = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
            l3 = ["Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
            if n < 10:
                return l1[int(num)]
            if 10 <= n < 20:
                return l2[n - 10]
            if 20 <= n < 100:
                return l3[int(num[0]) - 2] + " " + l1[int(num[1])] if num[1] != "0" else l3[int(num[0]) - 2]
        res = ""
        if num >= 1000000000:
            res = helper(str(num)[0]) + " Billion"
            if str(num)[1:4] != "000":
                res += " " + helper(str(num)[1:4]) + " Million"
            if str(num)[4:7] != "000":
                res += " " + helper(str(num)[4:7]) + " Thousand"
            if str(num)[7:] != "000":
                res += " " + helper(str(num)[7:])
        elif num >= 1000000:
            res = helper(str(num)[:-6]) + " Million"
            if str(num)[-6:-3] != "000":
                res += " " + helper(str(num)[-6:-3]) + " Thousand"
            if str(num)[-3:] != "000":
                res += " " + helper(str(num)[-3:])
        elif num >= 1000:
            res = helper(str(num)[:-3]) + " Thousand"
            if str(num)[-3:] != "000":
                res += " " + helper(str(num)[-3:])
        else:
            return helper(str(num))
        return res
举报

相关推荐

0 条评论