LintCode_524_左填充

来源:互联网 发布:greengene数据库 编辑:程序博客网 时间:2024/04/28 11:19

问题描述

实现一个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=' ') {        return string(max(0, size - static_cast<int>(originalStr.length())),                      padChar).append(originalStr);    }};