LintCode - 524.左填充

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 18:54   1175   0


实现一个leftpad库,如果不知道什么是leftpad可以看样例

样例
leftpad("foo", 5)
>> "  foo"

leftpad("foobar", 6)
>> "foobar"

leftpad("1", 2, "0")
>> "01"

class StringUtils {
public:
    /**
     * @param originalStr the string we want to append to
     * @param size the target length of the string
     * @param padChar the character to pad to the left side of the string
     * @return a string
     */
    static string leftPad(string& originalStr, int size, char padChar=' ') {
        // Write your code here
        int len = originalStr.size();
        
        if( len < size ) {
            string add( size - len, padChar );
            originalStr = add + originalStr;
        }
        
        return originalStr;
    }
};


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP