英文:
One code is working while after a small change code is not working
问题
The overflow error is occurring in the modified code because of the change made in line no. 7:
if((ans.length() - 1 >= 0) && s[i] == ans[ans.length()-1]){ // line no.7
In this modified line, you are checking if (ans.length() - 1 >= 0)
before accessing ans[ans.length()-1]
. This check is intended to prevent accessing elements of an empty string when ans
is empty. However, the check itself has a potential issue that can lead to an overflow error.
The issue is that ans.length()
is of type size_t
, which is an unsigned integer type. When ans
is empty (i.e., ans.length()
is 0), and you subtract 1 from it (ans.length() - 1
), you get a very large positive number due to underflow, rather than -1 as you might expect. This can lead to unexpected behavior and runtime errors.
To fix this issue and prevent overflow errors, you should modify the condition like this:
if(!ans.empty() && s[i] == ans.back()){
In this corrected condition, we use !ans.empty()
to check if ans
is not empty, and we use ans.back()
to access the last character of ans
. This avoids the underflow issue and correctly checks if ans
is not empty before accessing its last element.
英文:
Question = leetcode question = 1047
Remove All Adjacent Duplicates In String - LeetCode
> 1047. Remove All Adjacent Duplicates In String
>
> You are given a string s
consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.
>
> We repeatedly make duplicate removals on s
until we no longer can.
>
> Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
>
> Example 1:
>
> Input: s = "abbaca"
> Output: "ca"
> Explanation:
> For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
>
> Example 2:
>
> Input: s = "azxxzy"
> Output: "ay"
>
> Constraints:
>
> - 1 <= s.length <= 105
> - s
consists of lowercase English letters.
This code is working
class Solution {
public:
string removeDuplicates(string s) {
int n = s.length();
string ans = "";
for(int i=0;i<n;i++){
if((ans.length() > 0) && s[i] == ans[ans.length()-1]){
ans.pop_back();
}
else{
ans.push_back(s[i]);
}
}
return ans;
}
};
after a small change in line no.7
ans.length() > 0
to ans.length() - 1 >= 0
, code is not working
class Solution {
public:
string removeDuplicates(string s) {
int n = s.length();
string ans = "";
for(int i=0;i<n;i++){
if((ans.length() - 1 >= 0) && s[i] == ans[ans.length()-1]){ // line no.7
ans.pop_back();
}
else{
ans.push_back(s[i]);
}
}
return ans;
}
};
overflow error is showing. Why this is happening
答案1
得分: 5
因为 unsigned
整数 (length()
)
ans.length() - 1 >= 0
总是为 true
。 (0u - 1 == std::numeric_limits<unsigned>::max()
)
你可以改成:
ans.length() >= 1
或 !ans.empty()
。
你可以完全避免索引,使用 for-range 和适当的方法:
std::string removeDuplicates(std::string& s) {
std::string ans = "";
for (auto c : s){
if (!ans.empty() && c == ans.back()) {
ans.pop_back();
} else {
ans.push_back(c);
}
}
return ans;
}
英文:
Because of unsigned
integer (length()
)
ans.length() - 1 >= 0
is always true
. (0u - 1 == std::numeric_limits<unsigned>::max()
)
You might change to:
ans.length() >= 1
or !ans.empty()
.
You might avoid index completely with for-range and appropriate method:
std::string removeDuplicates(std::string& s) {
std::string ans = "";
for (auto c : s){
if (!ans.empty() && c == ans.back()) {
ans.pop_back();
} else {
ans.push_back(c);
}
}
return ans;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论