[英]Reverse a string. Not sure why this logic is wrong. C++
I am a beginner in solving algorithmic questions. 我是解决算法问题的初学者。 Until now, I have only self-taught coding. 到目前为止,我只有自学成才的编码。 So, I am not sure about the proper conventions. 因此,我不确定适当的约定。
I was trying to solve a question to reverse a string.There is some problem with the code but I am not sure what it is after debugging step-by-step. 我试图解决一个反向字符串的问题,代码有些问题,但是我不确定一步一步调试后是什么。
class Solution {
public:
string reverseString(string s) {
int n = s.length();
string reverse;
for (int i=0;i<s.length();i++)
{
reverse[i] = s[n-1];
n=n-1;
}
return reverse;
}
};
Input: "Hello" Output needed: "olleh" My output: "olleh " (extra space) 输入:“ Hello”需要的输出:“ olleh”我的输出:“ olleh”(多余的空格)
Input: A man, a plan, a canal: Panama Output: No output 输入:一个人,一个计划,一条运河:巴拿马输出:没有输出
I searched online for solutions. 我在网上搜索解决方案。 There were related to pointers. 与指针有关。 It would be great if someone helped me understand why this logic doesn't work and why using pointers is a better idea. 如果有人帮助我理解为什么这种逻辑不起作用以及为什么使用指针是一个更好的主意,那将是很好的。
ALREADY GIVEN. 已经给予。 CANNOT CHANGE: 不能改变:
string stringToString(string input) {
assert(input.length() >= 2);
string result;
for (int i = 1; i < input.length() -1; i++) {
char currentChar = input[i];
if (input[i] == '\\') {
char nextChar = input[i+1];
switch (nextChar) {
case '\"': result.push_back('\"'); break;
case '/' : result.push_back('/'); break;
case '\\': result.push_back('\\'); break;
case 'b' : result.push_back('\b'); break;
case 'f' : result.push_back('\f'); break;
case 'r' : result.push_back('\r'); break;
case 'n' : result.push_back('\n'); break;
case 't' : result.push_back('\t'); break;
default: break;
}
i++;
} else {
result.push_back(currentChar);
}
}
return result;
}
int main() {
string line;
while (getline(cin, line)) {
string s = stringToString(line);
string ret = Solution().reverseString(s);
string out = (ret);
cout << out << endl;
}
return 0;
}
Reversing a string is trivial. 反转字符串很简单。 Just construct a new one from the reverse iterators: 只需从反向迭代器构造一个新的:
std::string reverse_str(s.rbegin(), s.rend());
or 要么
std::string reverse_str(s.crbegin(), s.crend());
Here's how I would write your function: 这是我编写您的函数的方式:
string reverseString(const string& s) {
return {s.crbegin(), s.crend()};
}
As you create reverse
, you have to pass the length of the string as an argument, else the created string will be of size 0. This could look like this: 创建reverse
,必须将字符串的长度作为参数传递,否则创建的字符串的大小将为0。这看起来像这样:
string reverseString(string s) {
int n = s.length();
string reverse(n,'0');
for (int i=0;i<s.length();i++)
{
reverse[i] = s[n-1];
n=n-1;
}
return reverse;
}
Try this out 试试看
class Solution {
public:
string reverseString(string s) {
//cout<<"inside func";
int n = s.length();
cout<<n<<endl;
char reverse[sizeof(char)*n];// reverse stores the reverse of original string s
int i= 0;
for ( i=0;i<s.length();i++)
{
reverse[i] = s[n-i-1];
}
return reverse;
}
}
int main()
{
string s,r;
Solution sol;
s= "hello";
r= sol.reverseString(s);
cout<<r<<endl;
cout<<r.length()<<endl;
return 0;
}
when i= 0, ni-1= n-1 which is the last element of the original string s. 当i = 0时,ni-1 = n-1,它是原始字符串s的最后一个元素。 So the first element of the reverse string is the last element of s. 因此,反向字符串的第一个元素是s的最后一个元素。 Next i becomes i+1 ie 1. This time second element of the reverse string is the last but one element in string s. 接下来,i变为i + 1,即1。这一次,反向字符串的第二个元素是字符串s中的最后一个元素。 This procedure is repeated till i < s.length(). 重复此过程,直到i <s.length()。 The element to get copied is for index i= n-1 and n becomes n-(n-1)-1= 0, so the last element of reverse string is the first element of s. 要复制的元素是针对索引i = n-1,并且n变为n-(n-1)-1 = 0,因此反向字符串的最后一个元素是s的第一个元素。 After this the loop exists. 此后,循环存在。 No additional extra characters are added. 没有添加其他额外的字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.