defisPalindrome2(self, x: int) -> bool: if x < 0: returnFalse rev, original = 0, x while x > 0: rev = rev * 10 + x % 10# 121 x = x // 10# 1 return rev == original
The demons had captured the princess and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of m x n rooms laid out in a 2D grid. Our valiant knight was initially positioned in the top-left room and must fight his way through dungeon to rescue the princess.
The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight’s health (represented by positive integers).
To reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
Return the knight’s minimum initial health so that he can rescue the princess.
Note that any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
Example 1:
Input: dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]] Output: 7 Explanation: The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
Example 2:
Input: dungeon = [[0]] Output: 1
Constraints:
m == dungeon.lengthn == dungeon[i].length 1 <= m, n <= 200-1000 <= dungeon[i][j] <= 1000
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
SymbolValue I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, 2 is written as II in Roman numeral, just two one’s added together. 12 is written as XII, which is simply X + II. The number 27 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.
Example 1:
Input: s = “III” Output: 3 Explanation: III = 3.
Example 2:
Input: s = “LVIII” Output: 58 Explanation: L = 50, V= 5, III = 3.
Example 3:
Input: s = “MCMXCIV” Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:1 <= s.length <= 15 s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
It is guaranteed that s is a valid roman numeral in the range [1, 3999].
题目大意:
罗马数组转阿拉伯数字
解题思路:
按照规则累加。有一个特别规则是需要做减法如IV。
解题步骤:
N/A
注意事项:
先加再减的方法。
SYMBOL_TO_VAL的值可以哟用于判断先后顺序。
Python代码:
1 2 3 4 5 6 7 8 9 10
defromanToInt(self, s: str) -> int: SYMBOL_TO_VAL = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} res, num, prev = 0, 0, '' for symbol in s: num = SYMBOL_TO_VAL[symbol] if prev and SYMBOL_TO_VAL[prev] < SYMBOL_TO_VAL[symbol]: res -= SYMBOL_TO_VAL[prev] * 2 res += num prev = symbol return res
Given two sparse vectors, compute their dot product.
Implement class SparseVector:
SparseVector(nums) Initializes the object with the vector numsdotProduct(vec) Compute the dot product between the instance of SparseVector and vec
A sparse vector is a vector that has mostly zero values, you should store the sparse vector efficiently and compute the dot product between two SparseVector.
Follow up: What if only one of the vectors is sparse?
def__init__(self, nums: List[int]): self.idx_to_num = collections.defaultdict(int) for i, n in enumerate(nums): if n > 0: self.idx_to_num[i] = n
# Return the dotProduct of two sparse vectors defdotProduct(self, vec: 'SparseVector') -> int: res = 0 for i, n in vec.idx_to_num.items(): if i in self.idx_to_num: res += self.idx_to_num[i] * n return res
def__init__(self, nums: List[int]): self.non_zero_list = [] for i, n in enumerate(nums): if n > 0: self.non_zero_list.append((i,n))
# Return the dotProduct of two sparse vectors defdotProduct(self, vec: 'SparseVector') -> int: i, j, res = 0, 0, 0 while i <= len(self.non_zero_list) - 1and j <= len(vec.non_zero_list) - 1: if self.non_zero_list[i][0] == vec.non_zero_list[j][0]: res += self.non_zero_list[i][1] * vec.non_zero_list[j][1] i += 1 j += 1 elif self.non_zero_list[i][0] < vec.non_zero_list[j][0]: i += 1 else: j += 1 return res