码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Leetcode 29. Divide Two Integers (Python)


    Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.

    The integer division should truncate toward zero, which means losing its fractional part. For example, 8.345 would be truncated to 8, and -2.7335 would be truncated to -2.

    Return the quotient after dividing dividend by divisor.

    Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For this problem, if the quotient is strictly greater than 231 - 1, then return 231 - 1, and if the quotient is strictly less than -231, then return -231.

    Example 1:

    Input: dividend = 10, divisor = 3
    Output: 3
    Explanation: 10/3 = 3.33333.. which is truncated to 3.
    

    Example 2:

    Input: dividend = 7, divisor = -3
    Output: -2
    Explanation: 7/-3 = -2.33333.. which is truncated to -2.
    

    Constraints:

    • -231 <= dividend, divisor <= 231 - 1
    • divisor != 0
    1. class Solution(object):
    2. def divide(self, dividend, divisor):
    3. """
    4. :type dividend: int
    5. :type divisor: int
    6. :rtype: int
    7. """
    8. if dividend == 0:
    9. return 0
    10. shang = float(dividend)/float(divisor)
    11. shang1 = dividend/divisor
    12. if shang < 0 and shang > shang1:
    13. return shang1 + 1
    14. if shang > 2**31-1:
    15. return 2**31-1
    16. if shang < -2**31:
    17. return -2**31
    18. return shang1

  • 相关阅读:
    数据结构—堆(C语言实现)
    什么是全链路压测?
    C语言和C++的区别
    推荐给前端开发的 5 款 Chrome 扩展
    WXML模板语法
    将JSON数据转换成JAVA的实体类
    消息中间件:Puslar、Kafka、RabbigMQ、ActiveMQ
    python-opencv使用摄像头实时人脸检测
    app小程序手机端Python爬虫实战08-UiSelector坐标定位方式和控件操作超时问题
    【自动化测试框架】关于unitttest你需要知道的事
  • 原文地址:https://blog.csdn.net/weixin_58080442/article/details/125572159
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号