前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python3刷题系列(二)

Python3刷题系列(二)

作者头像
用户5473628
发布2019-08-08 14:50:14
4760
发布2019-08-08 14:50:14
举报
文章被收录于专栏:MiningAlgorithmsMiningAlgorithms

1,数组:

Three Sum(求三数之和)

英文版:https://leetcode.com/problems/3sum/

中文版:https://leetcode-cn.com/problems/3sum/

Majority Element(求众数)

英文版:https://leetcode.com/problems/majority-element/

中文版:https://leetcode-cn.com/problems/majority-element/

见证python暴力的时候到了,一行代码搞定:

代码语言:javascript
复制
return sorted(nums)[int(len(nums)/2)]

Missing Positive(求缺失的第一个正数)

英文版:https://leetcode.com/problems/first-missing-positive/

中文版:https://leetcode-cn.com/problems/first-missing-positive/

2,字符串:

Reverse String (反转字符串)

英文版:https://leetcode.com/problems/reverse-string/

中文版:https://leetcode-cn.com/problems/reverse-string/

用python自带的函数反而是用时最少的,第一种放大是自建循环,用时居中,172s。

Reverse Words in a String(翻转字符串里的单词)

英文版:https://leetcode.com/problems/reverse-words-in-a-string/

中文版:https://leetcode-cn.com/problems/reverse-words-in-a-string/

String to Integer (atoi)(字符串转换整数 (atoi))

英文版:https://leetcode.com/problems/string-to-integer-atoi/

中文版:https://leetcode-cn.com/problems/string-to-integer-atoi/

代码语言:javascript
复制
class Solution:
    def myAtoi(self, s):
        """
        :type str: str
        :rtype: int
        """
        s = s.lstrip()
        length = len(s)
        if length == 0 or s[0] not in '0123456789+-':
            return 0
        index = 1
        while index < length and s[index].isdigit():
            index += 1
        if index == 1 and s[0] in '+-':
            return 0
        result = int(s[:index])
        if result < 0:
            return max(-2147483648, result)
        else:
            return min(2147483647, result)

Reference:

Leetcode-15: https://www.jianshu.com/p/dd8dcdd2baf8

Leetcode-41: https://blog.csdn.net/weixin_40449300/article/details/82532996

Leetcode-151: https://blog.csdn.net/sxingming/article/details/51597524

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-04-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 MiningAlgorithms 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1,数组:
  • 2,字符串:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档