Meta OA 面试真题解析:Palindrome Check and Continuous Subsequence Sum

17次阅读
没有评论

Question Statement

Write a function that returns true if a given string is a palindrome (a palindrome is a string that is the same when reversed, if you ignore punctuation and capitalization). Some examples of palindromes are:

  • Race car!
  • A man, a plan, a canal, Panama!

Question

Given a sequence of positive integers and a positive integer total target, return whether a continuous sequence of integers sums up to target.

Example

  • [1, 3, 1, 4, 23], 8 : True (because 3 + 1 + 4 = 8)
  • [1, 3, 1, 4, 23], 7 : False

这道题其实包含两个常见面试考点:第一个是回文字符串判断,需要先忽略大小写和标点,再用双指针从两端向中间比较;第二个是连续子数组求和判断,给定正整数数组和目标值,判断是否存在一段连续区间的和正好等于目标。由于数组元素为正数,通常可以用滑动窗口在 O(n) 时间内解决:右指针扩展窗口累加,窗口和超过目标时左指针收缩,直到找到答案或遍历结束。

正文完
 0