实现一个leftpad库,如果不知道什么是leftpad可以看样例
样例
例1:
输入:
leftpad("foo", 5)
输出:
" foo"
例2:
输入:
leftpad("foobar", 6)
输出:
"foobar"
例3:
输入:
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();
string result;
for (int i = 0; i < size-len; i++) {
/* code */
result+=padChar;
}
result+=originalStr;
return result;
}
};