美文网首页
LeetCode #171 #860 #717 #237 #13

LeetCode #171 #860 #717 #237 #13

作者: 40巨盗 | 来源:发表于2018-09-11 23:24 被阅读0次

    171. Excel Sheet Column Number

    https://leetcode.com/problems/excel-sheet-column-number/description/

    利用ord方法求出当前字符代表的数值,每次用之前的数值乘以26再加上当前数值即可。
    代码如下:

    class Solution:
        def titleToNumber(self, s):
            """
            :type s: str
            :rtype: int
            """
            from functools import reduce
            return reduce(lambda x, y: x * 26 + y, [ord(char) - ord('A') + 1 for char in s])
    

    860. Lemonade Change

    https://leetcode.com/problems/lemonade-change/description/

    收到5元和10元都很好理解,只有一种选择;收到20元的时候如果有10元优先找10元给顾客,如果没有十元再找3个5元。
    代码如下:

    class Solution:
        def lemonadeChange(self, bills):
            """
            :type bills: List[int]
            :rtype: bool
            """
            five, ten = 0, 0
            for num in bills:
                if num == 5:
                    five += 1
                elif num == 10:
                    five -= 1
                    ten += 1
                    if five < 0:
                        return False
                else:
                    if ten > 0 and five > 0:
                        ten -= 1
                        five -= 1
                    elif ten == 0 and five >= 3:
                        five -= 3
                    else:
                        return False
            return True
    

    717. 1-bit and 2-bit Characters

    https://leetcode.com/problems/1-bit-and-2-bit-characters/description/

    由于最后一位一定为0,所以从倒数第二位开始判断即可。
    如果倒数第二位是0,那么最后一位一定是1-bit,返回True.
    如果不为0,那就一直往前搜索,直到找到第一个0为止,记录遇到1的数量。如果为奇数,说明是2-bit;如果为偶数,说明是1-bit.

    We don't need to traverse the whole array, just check the last part of it.
    if there is only one symbol in array the answer is always true (as last element is 0)
    if there are two 0s at the end again the answer is true no matter what the rest symbols are( ...1100, ...1000,)
    if there is 1 right before the last element(...10), the outcome depends on the count of sequential 1, i.e.
    a) if there is odd amount of 1(10, ...01110, etc) the answer is false as there is a single 1 without pair.
    b) if it's even (110, ...011110, etc) the answer is true, as 0 at the end doesn't have anything to pair with.

    代码如下:

    class Solution:
        def isOneBitCharacter(self, bits):
            """
            :type bits: List[int]
            :rtype: bool
            """
            ones = 0
            for i in range(len(bits))[len(bits) - 2::-1]:
                if bits[i] == 0:
                    break
                ones += 1
            return ones % 2 == 0
    

    237. Delete Node in a Linked List

    https://leetcode.com/problems/intersection-of-two-arrays/description/

    我们没法删除当前node,但是我们可以取巧的把下一个node的值复制到当前node,然后删掉下一个node,相当于删掉当前node.
    代码如下:

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def deleteNode(self, node):
            """
            :type node: ListNode
            :rtype: void Do not return anything, modify node in-place instead.
            """
            node.val = node.next.val
            node.next = node.next.next
    

    13. Roman to Integer

    https://leetcode.com/problems/roman-to-integer/description/

    如果当前字符比下一个字符代表的数值小,需要减去;
    如果当前字符比下一个字符代表的数值大,需要加上;
    由于最后一个字符代表的数值一定需要加上,所以可以单独处理,还可以避免越界的判断条件。
    代码如下:

    class Solution:
        def romanToInt(self, s):
            """
            :type s: str
            :rtype: int
            """
            res = 0
            value_dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
            for i in range(len(s) - 1):
                if value_dict[s[i]] < value_dict[s[i + 1]]:
                    res -= value_dict[s[i]]
                else:
                    res += value_dict[s[i]]
            return res + value_dict[s[-1]]
    

    相关文章

      网友评论

          本文标题:LeetCode #171 #860 #717 #237 #13

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