条件检查布尔值时会出现警告

huangapple go评论159阅读模式
英文:

Bool condition checks gives warning

问题

我正在尝试解决一个问题,链接在 https://leetcode.com/problems/word-break/ 。我的代码如下:-

  1. bool existsInDict(string s, vector<string>& wordDict)
  2. {
  3. if(std::find(wordDict.begin(),wordDict.end(),s) != wordDict.end())
  4. {
  5. return true;
  6. }
  7. return false;
  8. }
  9. class Solution {
  10. public:
  11. bool wordBreak(string s, vector<string>& wordDict) {
  12. int str_size = s.length();
  13. if(str_size == 0)
  14. return true;
  15. bool *dict = new bool[str_size+1];
  16. std::fill(dict, dict+str_size,false);
  17. for(int i =1;i<=str_size;++i)
  18. {
  19. if(dict[i]==false && existsInDict(s.substr(0,i),wordDict))
  20. {
  21. dict[i] = true;
  22. }
  23. if(dict[i]==true)
  24. {
  25. if(i==str_size)
  26. return true;
  27. for(int j=i+1;j<=str_size;++j)
  28. {
  29. if((dict[j]==false) && existsInDict(s.substr(i+1,j-i),wordDict))
  30. {
  31. dict[j] = true;
  32. }
  33. if((dict[j]==true) && (j == str_size))
  34. {
  35. return true;
  36. }
  37. }
  38. }
  39. }
  40. return false;
  41. }
  42. };

这给我一个错误 Line 40: Char 25: runtime error: load of value 190, which is not a valid value for type 'bool' (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:49:25

我不确定这里有什么问题,因为在那一行的 if 循环中,我两个检查都有一个布尔结果。有人可以帮助我理解吗?

谢谢。

英文:

I am trying to solve a problem here https://leetcode.com/problems/word-break/ . My code looks like below:-

  1. bool existsInDict(string s, vector&lt;string&gt;&amp; wordDict)
  2. {
  3. if(std::find(wordDict.begin(),wordDict.end(),s) != wordDict.end())
  4. {
  5. return true;
  6. }
  7. return false;
  8. }
  9. class Solution {
  10. public:
  11. bool wordBreak(string s, vector&lt;string&gt;&amp; wordDict) {
  12. int str_size = s.length();
  13. if(str_size == 0)
  14. return true;
  15. bool *dict = new bool[str_size+1];
  16. std::fill(dict, dict+str_size,false);
  17. for(int i =1;i&lt;=str_size;++i)
  18. {
  19. if(dict[i]==false &amp;&amp; existsInDict(s.substr(0,i),wordDict))
  20. {
  21. dict[i] = true;
  22. }
  23. if(dict[i]==true)
  24. {
  25. if(i==str_size)
  26. return true;
  27. for(int j=i+1;j&lt;=str_size;++j)
  28. {
  29. if((dict[j]==false) &amp;&amp; existsInDict(s.substr(i+1,j-i),wordDict))
  30. {
  31. dict[j] = true;
  32. }
  33. if((dict[j]==true) &amp;&amp; (j == str_size))
  34. {
  35. return true;
  36. }
  37. }
  38. }
  39. }
  40. return false;
  41. }
  42. };

This gives me a error Line 40: Char 25: runtime error: load of value 190, which is not a valid value for type &#39;bool&#39; (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:49:25

I am not sure what is wrong here as both my checks in the if loop on that line have a bool outcome. Can someone help me understand it ?

Thanks

答案1

得分: 1

你正在检查dict[j]是否为true,并且j是否等于str_size,然而,当j等于str_size时出现问题,所以我认为你必须修改循环条件为j &lt; str_size,而不是j &lt;= str_size,因为这可以确保j保持在dict数组的范围内!

请像下面这样修复它:

  1. if ((dict[j] == true) && (j == str_size - 1))
英文:

you are checking if dict[j] is true and if j is equal to str_size, however,the problem occurs when j becomes equal to str_size, so I think you must modify the loop condition to j &lt; str_size instead of j &lt;= str_size because it ensures you the j remains within the bounds of the dict array!

so fix it like below :

  1. if ((dict[j] == true) &amp;&amp; (j == str_size - 1))

huangapple
  • 本文由 发表于 2023年8月5日 16:04:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定