简体   繁体   English

Dart “For 循环” “If Else” “布尔返回”

[英]Dart “For Loop” “If Else” “Boolean return”

To call my self a noob would make noobs look bad... lets have a look at the code first...称我自己为菜鸟会让菜鸟看起来很糟糕......让我们先看看代码......

bool isPalindrome(String s){  
  for(int i = 0; i < s.length/2;i++){
    if(s[i] != s[(s.length-1) -i])
      return false;        
  }  
  return true;  
}

main() {
  print(isPalindrome("anna"));
  print(isPalindrome("cat"));
}

... Results true - false ...结果真 - 假

I came across that in a list of Dart examples for "beginners" what is s.length even if i change the number of letters in "cat" to "cats" it's "false", so it's not counting the number of letters but "caac" is "true".我在“初学者”的 Dart 示例列表中遇到了s.length是什么,即使我将"cat"中的字母数量更改为"cats"它是“假”,所以它不是计算字母的数量而是"caac"是“真实的”。 Can someone please give an in depth explanation of what that code is doing.有人可以深入解释该代码在做什么。

Your misunderstanding isn't in the code, but in what a "palindrome" is.您的误解不在于代码,而在于“回文”是什么。 A palindrome is a word that is spelled the same forwards and reversed.回文是一个正向和反向拼写相同的单词。 For example, if you took the word "racecar" and reversed the order of the letters, the result would be "racecar", ie the exact same word.例如,如果您取单词“racecar”并将字母顺序颠倒,结果将是“racecar”,即完全相同的单词。

The code returns true for "anna" because this is still true - "anna" backward is again "anna".代码为“anna”返回真,因为这仍然是真 - 向后的“anna”又是“anna”。 It returns false for "cat" because spelled backward it is "tac", which is a completely different word thus demonstrating that "cat" is not a palindrome.它为“cat”返回 false,因为向后拼写是“tac”,这是一个完全不同的词,因此表明“cat”不是回文。 (Likewise, "caac" backward is "caac" and thus a palindrome, and "cats" backward is "stac" and thus not a palindrome.) (同样,“caac”向后是“caac”,因此是回文,而“cats”向后是“stac”,因此不是回文。)

The way the code works is it compares every letter in the word with the letter in the mirrored position.代码的工作方式是将单词中的每个字母与镜像 position 中的字母进行比较。 For example, the mirrored position of the first letter is the last letter, the second letter is the second-to-last letter, and so on.例如,第一个字母的镜像position是最后一个字母,第二个字母是倒数第二个字母,以此类推。 If the letters are the same, the loop continues on.如果字母相同,则循环继续。 If it reaches the end and every letter compared is the same, then the word is spelled the same forward and backward, meaning it's a palindrome.如果它到达末尾并且比较的每个字母都相同,则该单词的前后拼写相同,这意味着它是回文。

V       
racecar  ==Same letter, continuing==
      ^ 

 V
racecar  ==Same letter, continuing==
     ^

  V
racecar  ==Same letter, continuing==
    ^

   V
racecar  ==Same letter, continuing==
   ^

==The end has been reached, the word is a palindrome, return true==

If however, it ever reaches a point where a letter and the letter in the opposite position are not the same, that means the word would not be spelled the same backward as it is forward, which means it is not a palindrome.但是,如果它曾经达到一个字母和对面 position 中的字母不相同的程度,则意味着该单词的向后拼写与向前拼写不同,这意味着它不是回文。

V
abcaa  ==Same letter, continuing==
    ^

 V
abcaa  ==Different letter, the word is not a palindrome, return false==
   ^

(The reason in the loop it only goes up to length/2 is because once the check passes the halfway mark, it will start comparing letters it already compared. ie in "abcde", comparing "b" to "d" is the same as comparing "d" to "b", so the loop cuts out halfway to avoid unnecessary comparisons.) (在循环中它只上升到length/2的原因是因为一旦检查通过中间标记,它将开始比较它已经比较过的字母。即在“abcde”中,比较“b”和“d”是相同的因为比较“d”和“b”,所以循环中途切断以避免不必要的比较。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM