一、出错代码和经验

class Solution {
public:
    string defangIPaddr(string address) {

        int position=1;
        //错误
        while( npos!=(position=address.rfind('.')) )
        {
            address.replace(position,1,"[.]");
        }

        return address;
    }
};

显示

Line 6: Char 16: error: use of undeclared identifier 'npos'; did you mean 'fpos'?
        while( npos!=(position=address.rfind('.')) )
               ^~~~
               fpos
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/postypes.h:112:11: note: 'fpos' declared here
    class fpos
          ^

注意,那个npos不是这么用的,需要用::访问,尴尬,C++基础都忘记了。。。

二、修改后AC的代码

使用的:rfind和replace成员函数的使用

class Solution {
public:
    string defangIPaddr(string address) {

        int position=address.size();
        while( string::npos !=(position=address.rfind('.',position)) )
        {
            //从position开始,长度为1的被后面的代替!!!
            address.replace(position,1,"[.]");
        }

        return address;
    }
};

二、用insert的,故意用insert的学习

https://leetcode-cn.com/problems/defanging-an-ip-address/solution/javasan-chong-jie-fa-di-san-chong-shuang-100jie-ju/