【问题标题】:(str1[i] || str2[i]) what does this mean, can someone explain(str1[i] || str2[i]) 这是什么意思,谁能解释一下
【发布时间】:2019-12-15 06:01:24
【问题描述】:

我试图找出两个字符串是否是字谜,这些代码验证它们是否长度相等。怎么样?

【问题讨论】:

  • 这是什么语言?
  • 语言是 C++。 str1 和 str2 是两个字符串, (str1[i] || str2[i]) 是检查两个字符串是否等长。我只是想知道这个逻辑是如何工作的

标签: string anagram


【解决方案1】:

str1 是一个 (char) 数组。 str2 是一个 (char) 数组。

此代码检查是否:

索引i 存在于str1
-或-
索引i 存在于str2

如果你的两个数组是
['a', 'b', 'c']
['w', 'x', 'y', 'z']

它可能会首先检查索引 0

i=0 // 'a' exists
i=1 // 'b' exists
i=3 // 'c' exists
i=4 // 'z' exists - first didn't match, so checks second
i=5 // doesn't exist
i=6 // doesn't exist
i=7 // doesn't...

【讨论】:

  • 如何保证字符串长度相等?
  • @Payal04 它没有
  • @Payal04 我发布了代码的作用,它不检查长度是否相等。
猜你喜欢
  • 1970-01-01
  • 2019-06-10
  • 1970-01-01
  • 2021-05-30
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多