41-45题

作者: yy辰 | 来源:发表于2018-10-14 13:04 被阅读13次

41、二位数组中的查找
比较简单

# -*- coding:utf-8 -*-
class Solution:
    # array 二维列表
    def Find(self, target, array):
        # write code here
        height = len(array)
        length = len(array[0])
        i = length - 1
        j = 0
        while i >= 0 and j <= height:
            if array[j][i] == target:
                return True
            elif array[j][i] > target:
                i -= 1
            elif array[j][i] < target:
                j += 1
        return False

42、扑克牌顺子
我的想法是如果数组除了0无重复元素,且除去0的最大值减最小值小于等于4的话那么就返回True。

# -*- coding:utf-8 -*-
class Solution:
    def IsContinuous(self, numbers):
        # write code here
        if not numbers:
            return False
        numbers.sort()
        cur_min = 13
        cur_max = 0
        temp = 0
        for i in numbers:
            if i != 0:
                if i == temp:
                    return False
                temp = i
                if i < cur_min:
                    cur_min = i
                if i > cur_max:
                    cur_max = i
        return cur_max-cur_min<=4

43、孩子们的游戏
自己用循环写的,调试了很久= =。

class Solution:
    def LastRemaining_Solution(self, n, m):
        # write code here
        temp = list(range(n))
        cur = -1
        index = -1
        while len(temp) > 1:
            cur += 1
            index += 1
            index = index if index < len(temp) else 0
            if cur == m-1:
                cur = -1
                temp.remove(temp[index])
                index -= 1
        return temp[0]

后来看别人的解法发现了这种问题有个专门的名词:约瑟夫环问题。仔细琢磨了一下,博客地址:https://www.cnblogs.com/cmmdc/p/7216726.html
结论:f[n] = (f[n-1] + m)%n
约瑟夫环代码

class Solution:
    def LastRemaining_Solution(self, n, m):
        # write code here
        if n == 1:
            return 0
        memory = [0]
        for i in range(2, n+1):
            memory.append((memory[-1] + m)%i)
        return memory[-1]

44、正则表达式匹配
分好情况,一类一类写就可以了,花了挺长时间,还是没写出来。先马住

45、表示数值的字符串

class Solution:
    def isNumeric(self, s):
        isAllowDot = True
        isAllowE = True
        for i in range(len(s)):
            if s[i] in "+-" and (i == 0 or s[i - 1] in "eE") and i < len(s) - 1:
                continue
            elif isAllowDot and s[i] == ".":
                isAllowDot = False
                if i >= len(s) - 1 or s[i + 1] not in "0123456789":
                    return False
            elif isAllowE and s[i] in "Ee":
                isAllowDot = False
                isAllowE = False
                if i >= len(s) - 1 or s[i + 1] not in "0123456789+-":
                    return False
            elif s[i] not in "0123456789":
                return False
        return True

相关文章

  • MySQL经典50题-第41到45题

    MySQL50-11-第41-45题 本文中介绍的是第41-45题,主要包含的知识点: 表的自连接查询比较信息 找...

  • 41-45题

    41、二位数组中的查找比较简单 42、扑克牌顺子我的想法是如果数组除了0无重复元素,且除去0的最大值减最小值小于等...

  • 41-45

  • 41-45

    人生就是在不停地选择,在自己的视野里,做对自己觉得最好的事情。究竟什么是好是坏呢?这个标准只有自己知道,也只有留给...

  • 2020幸福实修内外丰盛,家业同修11/100

    今日实修 ❤ 诵读《示弟立志说》&道德经41-45章 ❤️阅读《亲爱的安德烈》 ❤️今日觉察: 事件:原计划周末早...

  • 诗篇41-45

    诗篇 第41篇 〔大卫的诗,交与伶长。〕 眷顾贫穷的有福了!他遭难的日子,耶和华必搭救他!耶和华必保全他,使他存活...

  • 读《100个基本》有感-最基本的也是最有意义的(9)

    这篇文章是“100个基本”有感系列第九篇,将记录41-45条“基本”的感悟。 041 贯彻自己的意图。 “贯彻自己...

  • 诗词原创 观宋填词5首41-45 选冠子 青玉案 望江东 绿头鸭

    老街味道诗词原创,观宋填词系列41-45,五首词牌分别是:选冠子、青玉案、望江东、绿头鸭、调笑令。 观宋填词41•...

  • 晚安書 【41-45】

    这是封长信 于己自省 与你晚安 莫偶然 41 你认真想过用怎样的态度去生活,想象里方式很多,你觉得过程不重要。那时...

  • 【案例问答】41-45

    案例41 我深爱的男人一看见我跟别的男人说话就骂我“贱货”,说我不安分勾引男人。我不知如何是好? 回复: 如你所述...

网友评论

      本文标题:41-45题

      本文链接:https://www.haomeiwen.com/subject/xqlzaftx.html