Python 刷Leetcode题库,顺带学英语单词(3)

简介: Python 刷Leetcode题库,顺带学英语单词(3)

Palindrome Number


Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. [#9]


   Example 1:

   Input: 121

   Output: true

   Example 2:

   Input: -121

   Output: false

   Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

   Example 3:

   Input: 10

   Output: false

   Explanation: Reads 01 from right to left. Therefore it is not a palindrome.


Follow up:

Coud you solve it without converting the integer to a string?


题意:判断一个整数是否为回文数? 后续:能否不转成字符串就解决问题。

>>> result = lambda n:n==int(str(n)[::-1]) if n>=0 else False
>>> result(121)
True
>>> result(-121)
False
>>> result(10)
False
>>> 


如不允许转字符串,可以用%和//逐位取出数字再作判断。

>>> def isPalindrome(number):
  if number<0:
    return False
  res = []
  while number != 0:
    res.append(number%10)
    number //= 10
  return res==res[::-1]
>>> isPalindrome(121)
True
>>> isPalindrome(-121)
False
>>> isPalindrome(10)
False
>>> isPalindrome(0)
True
>>> 



Container With Most Water


Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai).n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. [#11]


Note: You may not slant the container and n is at least 2.


2021071914345028.png


The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

   Example:

   Input: [1,8,6,2,5,4,8,3,7]

   Output: 49


题意:找出序列中的元素所能围成矩形的最大面积值。


再次用到“子序列推导式”,只是本题不需要单个元素的子序列,很简单只要把后一个range()的第一参数i+1改成i+2:

>>> s=[1,2,3]
>>> [s[i:j] for i in range(len(s)) for j in range(i+1,len(s)+1)]
[[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]
>>> [s[i:j] for i in range(len(s)) for j in range(i+2,len(s)+1)]
[[1, 2], [1, 2, 3], [2, 3]]
>>> 



到得的子序列只要把首尾两元素的小者乘以(子序列长度-1)就是面积,其面积的最大值就是最终答案:

 

>>> maxArea = lambda s:max([min(t[0],t[-1])*(len(t)-1) for t in [s[i:j] for i in range(len(s)) for j in range(i+2,len(s)+1)]])
>>> L = [1,8,6,2,5,4,8,3,7]
>>> maxArea(L)
49
>>> 



Roman to Integer


Roman numerals are represented by seven different symbols: I , V , X , L , C , D and M .

Symbol Value


   I          1

   V         5

   X         10

   L          50

   C         100

   D         500

   M        1000


For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII , which is simply X + II . The number twenty seven is written as XXVII , which is XX + V + II .


Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII . Instead, the number four is written as IV . Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX .

There are six instances where subtraction is used:


I can be placed before V (5) and X (10) to make 4 and 9.

X can be placed before L (50) and C (100) to make 40 and 90.

C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.   [#13]


题意:把罗马数字的字符串转换成一个十进制整数。

>>> def roman2int(n):
  last = total = 0
  for i in range(len(n)):
    c = n[len(n)-(i+1):len(n)-i]
    num = c in 'IXCM' and 10**'IXCM'.index(c) or c in 'VLD' and 5*10**'VLD'.index(c)
    total += num*(-1 if num < last else 1)
    last = num
  return total
>>> roman2int('III')
3
>>> roman2int('IV')
4
>>> roman2int('IX')
9
>>> roman2int('LVIII')
58
>>> roman2int('MCMXCIV')
1994
>>> roman2int('MMXXI')
2021
>>> 



相关单词


目录
相关文章
|
5月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
65 6
|
5月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
130 2
|
3月前
|
JSON 数据格式 Python
Python实用记录(十四):python统计某个单词在TXT/JSON文件中出现的次数
这篇文章介绍了一个Python脚本,用于统计TXT或JSON文件中特定单词的出现次数。它包含两个函数,分别处理文本和JSON文件,并通过命令行参数接收文件路径、目标单词和文件格式。文章还提供了代码逻辑的解释和示例用法。
56 0
Python实用记录(十四):python统计某个单词在TXT/JSON文件中出现的次数
|
3月前
Leetcode(最后一个单词长度)
这篇文章介绍了两种解决LeetCode第58题的方法,即计算给定字符串中最后一个单词的长度,方法包括翻转字符串和逆向遍历统计。
23 0
|
3月前
【LeetCode 20】151.反转字符串里的单词
【LeetCode 20】151.反转字符串里的单词
27 0
|
3月前
|
小程序 IDE 开发工具
Python编写单词复习小程序
Python编写单词复习小程序
24 0
|
5月前
|
算法
LeetCode第58题最后一个单词的长度
LeetCode第58题"最后一个单词的长度"的解题方法,通过从字符串末尾向前遍历并计数非空格字符,直接得出最后一个单词的长度。
LeetCode第58题最后一个单词的长度
|
5月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
78 7
|
5月前
|
算法 Python
【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
本文介绍了LeetCode 2038题的解法,题目要求在一个由'A'和'B'组成的字符串中,按照特定规则轮流删除颜色片段,判断Alice是否能够获胜,并提供了Python的实现代码。
60 3
|
5月前
|
算法 Python
【Leetcode刷题Python】剑指 Offer 33. 二叉搜索树的后序遍历序列
本文提供了一种Python算法,用以判断给定整数数组是否为某二叉搜索树的后序遍历结果,通过识别根节点并递归验证左右子树的值是否满足二叉搜索树的性质。
28 3