1. Job Roadmap
  2. Home
  3. Problem Set
  4. codenotelist
  5. Forum
  6. course
  7. Shore Share Sessions
  8. Record
  1. Login
  2. Sign Up
  3. Language
    1. English
    2. 한국어
    3. 简体中文
    4. 正體中文
    ZhContent TextSol AI分析

思路

用按值二分在答案范围内搜索。记左右边界为 lll 与 rrr。当 x≥2x \ge 2x≥2 时有

image

因此可取初始区间 [l=1,r=⌊x2⌋+1][l=1,r=\left\lfloor \dfrac{x}{2} \right\rfloor+1][l=1,r=⌊2x​⌋+1];当 x<2x<2x<2 时直接返回 xxx。

在循环中取中点

P3451.第1题-求平方根

    1000ms Tried: 58 Accepted: 23 Difficulty: 2 所属公司 : B站
    算法与标签>二分算法

题目内容

实现函数 int mysqrt(int x) 。

计算并返回 xxx 的平方根(向下取整)

数据范围:0<=x<231−10<=x<2^{31}-10<=x<231−1

要求:空间复杂度 O(1)O(1)O(1) ,时间复杂度 O(logx)O(logx)O(logx)

样例1

输入

2

输出

1

样例2

输入

2143195649

输出

46294

登录后即可使用 AI 分析。

模式
倒计时时长
:

最长 10 小时 59 分;应用后按此时长重新开始。

提示:点击提交记录在左侧题面区域查看详情
题库
AI分析设置
留空使用官方API Key,每天有次数限制(自定义API Key仅限会员和管理员使用,不限次数)
会员和管理员可切换模型;切到 Kimi/智谱/通义/豆包时需填写对应供应商 API Key
升级会员,可将运行与提交冷却时间缩短至 1 秒起

Status

  • Judging Queue
  • Service Status

Development

  • Open Source

Support

  • Help
  • Contact Us

About

  • About
  • Privacy
  • Terms of Service
  • Copyright Complaint
  1. Language
    1. English
    2. 한국어
    3. 简体中文
    4. 正體中文
  2. Legacy mode
  3. Theme
    1. Light
    2. Dark
  1. 京ICP备2025123107号-1
  2. Worker 1, 47ms
  3. Powered by Hydro v5.0.0-beta.18 Community
CLOSE


ScanQRCodePrompt

请使用微信扫描下方二维码完成注册

Forgot password or username?