当前位置: 首页 > wzjs >正文

浙江省住房与城乡建设部网站阿里网站seo

浙江省住房与城乡建设部网站,阿里网站seo,仿抖音网站开发,门户网站英文版建设目录 一、881. 救生艇 贪心-排序-双指针 二、8. 字符串转换整数 (atoi) 1.模拟-未考虑溢出 2.考虑溢出问题 三、9. 回文数 1.双指针-字符串 2.数字翻转 3.数字翻转-只翻转一半 一、881. 救生艇 贪心-排序-双指针 class Solution:def numRescueBoats(self, people: Li…

目录

一、881. 救生艇

贪心-排序-双指针

二、8. 字符串转换整数 (atoi)

1.模拟-未考虑溢出

2.考虑溢出问题

三、9. 回文数

1.双指针-字符串

2.数字翻转

3.数字翻转-只翻转一半


一、881. 救生艇

贪心-排序-双指针

class Solution:def numRescueBoats(self, people: List[int], limit: int) -> int:# 贪心-排序-双指针n = len(people)people.sort()ans = 0if people[0] >= limit:return n left, right = 0, n - 1# while people[right] >= limit:     # 合在后面#     right -= 1#     ans += 1# 闭区间# 当l == r时,无论进哪里都是ans+1然后移动指针,跳出循环while left <= right:if people[left] + people[right] <= limit:# ans += 1left += 1right -= 1else:# ans += 1right -= 1ans += 1return ans

二、8. 字符串转换整数 (atoi)

1.模拟-未考虑溢出

class Solution:def myAtoi(self, s: str) -> int:# 模拟n = len(s)ans = 0idx = 0flag = 1# 处理前导空格while idx < n and s[idx] == ' ':idx += 1# 处理符号位if idx < n and (s[idx] == '+' or s[idx] == '-'):if s[idx] == '-':flag = -1idx += 1# 处理剩余位数while idx < n:if not s[idx].isdigit():breakans = ans * 10 + int(s[idx])idx += 1ans *= flagans = min(ans, 2 ** 31 - 1)ans = max(ans, - 2 ** 31)return ans

2.考虑溢出问题

来自题解(. - 力扣(LeetCode)),优雅。

class Solution:def myAtoi(self, s: str) -> int:# 考虑溢出问题ans, i, sign, n = 0, 0, 1, len(s)int_max, int_min, bndry = 2 ** 31 - 1, - 2 ** 31, 2 ** 31 // 10if not s: return 0      # 空字符串# 处理前导空格while s[i] == ' ':i += 1if i == n: return 0     # 字符串全为空# 处理符号位if s[i] == '-': sign = -1if s[i] in '+-': i += 1# 处理剩余位数for j in range(i, n):if not '0' <= s[j] <= '9': break# 溢出,同时考虑正负if ans > bndry or (ans == bndry and s[j] > '7'):return int_max if sign == 1 else int_minans = 10 * ans + ord(s[j]) - ord('0')return ans * sign

三、9. 回文数

1.双指针-字符串

class Solution:def isPalindrome(self, x: int) -> bool:# 双指针-字符串if x < 0: return False  # 负数肯定不是s = str(x)l, r = 0, len(s) - 1# 闭区间while l <= r:if s[l] != s[r]:return Falsel += 1r -= 1return True

2.数字翻转

class Solution:def isPalindrome(self, x: int) -> bool:# 数字翻转# 不使用字符串if x < 0: return False  # 负数肯定不是# 将数字进行逆序reverse_num = 0num = xwhile x > 0:reverse_num = reverse_num * 10 + x % 10x //= 10return reverse_num == num

3.数字翻转-只翻转一半

来自官方题解(. - 力扣(LeetCode))。

class Solution:def isPalindrome(self, x: int) -> bool:# 数字翻转-只翻转一半if x < 0 or (x % 10 == 0 and x != 0): return False  # 负数肯定不是,末尾为0的也不是# 这里有一点难想到特判末尾为0的情况# 将数字进行逆序reverse_num = 0# 当原始数字小于或等于反转后的数字时,说明已经处理了一半# 偶位数==,奇位数<while x > reverse_num:reverse_num = reverse_num * 10 + x % 10x //= 10return x == reverse_num or x == reverse_num // 10

感谢你看到这里!一起加油吧!

http://www.dtcms.com/wzjs/410103.html

相关文章:

  • 阿里巴巴网站如何做免费推广苏州吴中区seo关键词优化排名
  • 西红门模板网站建设新人跑业务怎么找客户
  • top后缀做网站好不好短视频seo询盘获客系统
  • 做网站运用的软件网站seo教材
  • 温州设计网站建设sem和seo的区别
  • 东台做网站的网上学电脑培训中心
  • 招标网站开发锦绣大地seo官网
  • 景观设计师做交通分析常用网站google官网进入
  • 南京网站建设哪里好seo文章推广
  • 开源网站统计游戏代理免费加盟
  • 企业网站模板包含什么搜索引擎优化的方式有哪些
  • 什么公司做网站会提供源代码湖人最新消息
  • 南昌互联网公司排名吉林seo基础
  • 上海做网站待遇58网络推广
  • 苏州网站制作设计嘉兴seo外包公司费用
  • 自建外贸推广网站有哪些做销售找客户渠道
  • 深圳网站公司如何免费推广网站
  • 网站建设的基本步奏株洲seo
  • 给WordPress添加视频播放页seo站长工具下载
  • 公司网站数据分析公司商洛网站建设
  • 郑州快速建站价格百度广告投放技巧
  • 唐山网站关键词优化淄博seo推广
  • 天津有哪些好的做网站公司谷歌优化排名哪家强
  • asp装修公司网站海外推广服务
  • 遵义建一个网站大概要多少钱培训课
  • 什么做网站赚钱教育培训机构网站
  • 申请网页空间的网站培训机构专业
  • 刷赞网站推广qq网站设计流程
  • 网站建设多少钱手机端搜索引擎排名
  • 儿童网站开发 论文500个游戏推广群