摘要:就是两个,一个有参数,一个没有,做法一样。用或放入前缀,直到新的字符串长度等于。
Problem
You know what, left pad is javascript package and referenced by React:
Github link
One day his author unpublished it, then a lot of javascript projects in the world broken.
You can see from github it"s only 11 lines.
You job is to implement the left pad function. If you do not know what left pad does, see examples below and guess.
Exampleleftpad("foo", 5) " foo" leftpad("foobar", 6) "foobar" leftpad("1", 2, "0") "01"Note
就是两个method,一个有参数padChar,一个没有,做法一样。用" "或padChar放入originalStr前缀,直到新的字符串长度等于size。
Solutionpublic class StringUtils { static public String leftPad(String originalStr, int size) { int len = originalStr.length(); StringBuilder sb = new StringBuilder(); if (size <= len) return originalStr; else { for (int i = 0; i < size-len; i++) sb.append(" "); sb.append(originalStr); } return sb.toString(); } static public String leftPad(String originalStr, int size, char padChar) { int len = originalStr.length(); StringBuilder sb = new StringBuilder(); if (size <= len) return originalStr; else { for (int i = 0; i < size-len; i++) sb.append(padChar); sb.append(originalStr); } return sb.toString(); } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/65804.html
摘要:复杂度思路参考的思路,对于,表示在从到的范围内,先手玩家能拿到的最大的硬币价值。对于状态,先手玩家有两种选择,要么拿的硬币,要么拿的硬币左边一个的或者右边一侧的,如果拿左侧的硬币,如果拿右侧的硬币,取两个值的最大值。 LintCode Coins in a line III There are n coins in a line. Two players take turns to ...
摘要:两个参赛者轮流从左边依次拿走或个硬币,直到没有硬币为止。计算两个人分别拿到的硬币总价值,价值高的人获胜。请判定第一个玩家是输还是赢样例给定数组返回给定数组返回复杂度思路考虑先手玩家在状态,表示在在第的硬币的时候,这一位玩家能拿到的最高价值。 LintCode Coins in a line II 有 n 个不同价值的硬币排成一条线。两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直...
摘要:使用另一个字符串填充字符串为指定长度该函数返回被从左端右端或者同时两端被填充到制定长度后的结果。如果的值是负数,小于或者等于输入字符串的长度,不会发生任何填充,并会返回。如果填充字符的长度不能被整除,那么可能会被缩短。 str_pad (PHP 4 >= 4.0.1, PHP 5, PHP 7) str_pad — Pad a string to a certain length w...
Problem Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You sho...
Problem Flatten a binary tree to a fake linked list in pre-order traversal.Here we use the right pointer in TreeNode as the next pointer in ListNode. Example 1 1 ...
阅读 1452·2019-08-30 15:55
阅读 1121·2019-08-30 15:52
阅读 1250·2019-08-29 13:53
阅读 1434·2019-08-29 11:19
阅读 2929·2019-08-26 13:29
阅读 508·2019-08-26 11:33
阅读 2552·2019-08-23 17:20
阅读 1000·2019-08-23 14:14