day7字符串

题目:

https://programmercarl.com/0344.反转字符串.html#算法公开课
解析:
https://programmercarl.com/0344.反转字符串.html
这道题能够独立做出来,我采用的是之前左指针不越过右指针的方法,而不是i < s.size()/2; 如下:

for(int i = 0, j = s.size() - 1; i <= j; i++, j--)
        {
            swap(s[i], s[j]);
        }

题目:

https://programmercarl.com/0541.反转字符串II.html#算法公开课
解析:
https://leetcode.cn/problems/reverse-string-ii/submissions/539303459/
这个是直接i作为动力采用2k的步长,这里没想到,我还想着计数去判断,计数还容易出问题,增加难度;
在从begin()+ i到begin() + i + k去转,这里挺巧妙的。

题目:

https://kamacoder.com/problempage.php?pid=1064
解析:
https://programmercarl.com/kamacoder/0054.替换数字.html
这里我犯的最愚蠢的错误一定要记录:

else {
                s[newIndex--] = s[oldIndex];
            }
            oldIndex--;
// 在这里我想着反正要--为啥不在内部--
else {
                s[newIndex--] = s[oldIndex--];
            }
// 想弄成这样,这样会报数组越界的问题,我想了半天才理解;原来是oldIndex是每次都要向左移一位;
而不是在不等于数字时才移,当棋等于数字完,也得向左移一位            
posted @   zhangenigma  阅读(7)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示