Press "Enter" to skip to content

Posts tagged as “string_view”

花花酱 LeetCode 2023. Number of Pairs of Strings With Concatenation Equal to Target

方法1: Brute Force

枚举所有的(nums[i], nums[j])组合,相加在和target比较。

时间复杂度:O(mn2) m为字符串的最长长度。
空间复杂度:O(m)

优化前 67ms, 49.3M

一些工程上的优化

  • 用string_view作为参数类型,减少一次string copy
  • 先比较长度,再判断内容。
  • string_view.substr 是O(1)时间,和直接用strncmp比较内容是一样的。

优化后 3ms, 12.88MB