A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s, return trueif it is a palindrome, orfalseotherwise.
Example 1:
Input: s = “A man, a plan, a canal: Panama” Output: true Explanation: “amanaplanacanalpanama” is a palindrome.
Example 2:
Input: s = “race a car” Output: false Explanation: “raceacar” is not a palindrome.
Example 3:
Input: s = “ “ Output: true Explanation: s is an empty string “” after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome.
Constraints:
`1 <= s.length <= 2 105*s` consists only of printable ASCII characters.
题目大意:
求含非字母数字的字符串是否回文,字符串含空格,冒号等. Easy题
双指针解题思路(推荐):
回文首先考虑用相向双指针
解题步骤:
N/A
注意事项:
比较时,要转换成小写
外循环left < right条件要复制到内循环中
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12
defisPalindrome(self, s: str) -> bool: left, right = 0, len(s) - 1 while left < right: while left < right andnot s[left].isalnum(): left += 1 while left < right andnot s[right].isalnum(): right -= 1 if s[left].lower() != s[right].lower(): returnFalse left += 1 right -= 1 returnTrue
算法分析:
时间复杂度为O(n),空间复杂度O(1)
reverse法算法II解题思路:
reverse字符串比较
注意事项:
比较时,要转换成小写
Python代码:
1 2 3 4 5 6
defisPalindrome2(self, s: str) -> bool: res = '' for char in s: if char.isalpha() or char.isdigit(): res += char.lower() returnTrueif res == res[::-1] elseFalse
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
Your algorithm should run in O(n) complexity.
Example:
**Input:** [100, 4, 200, 1, 3, 2]
**Output:** 4
**Explanation:** The longest consecutive elements sequence is `[1, 2, 3, 4]`. Therefore its length is 4.
deflongestConsecutive(self, nums: List[int]) -> int: num_set = collections.Counter(nums) res = 0 for n in nums: if num_set[n] == 0: continue max_len = 1 num_set[n] = 0 i = n + 1 while i in num_set: num_set[i] = 0 max_len += 1 i += 1 i = n - 1 while i in num_set: num_set[i] = 0 max_len += 1 i -= 1 res = max(res, max_len) return res
You are given a 0-indexed 2D integer array questions where questions[i] = [points<sub>i</sub>, brainpower<sub>i</sub>].
The array describes the questions of an exam, where you have to process the questions in order (i.e., starting from question 0) and make a decision whether to solve or skip each question. Solving question i will earn you points<sub>i</sub> points but you will be unable to solve each of the next brainpower<sub>i</sub> questions. If you skip question i, you get to make the decision on the next question.
For example, given questions = [[3, 2], [4, 3], [4, 4], [2, 5]]:
If question 0 is solved, you will earn 3 points but you will be unable to solve questions 1 and 2. If instead, question 0 is skipped and question 1 is solved, you will earn 4 points but you will be unable to solve questions 2 and 3.
Return the maximum points you can earn for the exam.
Example 1:
Input: questions = [[3,2],[4,3],[4,4],[2,5]] Output: 5 Explanation: The maximum points can be earned by solving questions 0 and 3. - Solve question 0: Earn 3 points, will be unable to solve the next 2 questions - Unable to solve questions 1 and 2 - Solve question 3: Earn 2 points Total points earned: 3 + 2 = 5. There is no other way to earn 5 or more points.
Example 2:
Input: questions = [[1,1],[2,2],[3,3],[4,4],[5,5]] Output: 7 Explanation: The maximum points can be earned by solving questions 1 and 4. - Skip question 0 - Solve question 1: Earn 2 points, will be unable to solve the next 2 questions - Unable to solve questions 2 and 3 - Solve question 4: Earn 5 points Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.
You are given the root of a binary tree containing digits from 0 to 9 only.
Each root-to-leaf path in the tree represents a number.
For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123.
Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer.
A leaf node is a node with no children.
Example 1:
Input: root = [1,2,3] Output: 25 Explanation: The root-to-leaf path 1->2 represents the number 12. The root-to-leaf path 1->3 represents the number 13. Therefore, sum = 12 + 13 = 25.
Example 2:
Input: root = [4,9,0,5,1] Output: 1026 Explanation: The root-to-leaf path 4->9->5 represents the number 495. The root-to-leaf path 4->9->1 represents the number 491. The root-to-leaf path 4->0 represents the number 40. Therefore, sum = 495 + 491 + 40 = 1026.
Constraints: The number of nodes in the tree is in the range [1, 1000]. 0 <= Node.val <= 9 The depth of the tree will not exceed 10.