LeetCode #171 #860 #717 #237 #13 2018-09-11

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]]
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、新业态融合 个人体会 跨界喜剧人节目大家都有看过吗?娱乐圈的明星们早就开始用跨界来提升自我的关注度,收视...
    葆婴USANA廖瑜阅读 76评论 0 0
  • 运算符优先级汇总表https://developer.mozilla.org/zh-CN/docs/Web/Jav...
    南山码农阅读 257评论 0 0
  • 希望可以努力一点 再努力一点 一切都会变好,变的越来越好 很喜欢现在的状态,有目标才有生活 2017.2.20开学
    青城FM阅读 150评论 1 1
  • 01 昨天强度对现在的自己还是大了点,早上又有点起不来了,估摸着自己还是要保持最少7小时的睡眠时间比较好。 02 ...
    小妖Troy阅读 100评论 0 1
  • 信言不美,美言不信。 《老子》八十一章 句意:可信的话不一定中听,中听的话不一定可信。
    xcy无名阅读 426评论 0 0