成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

[LintCode] Left Pad

X1nFLY / 2346人閱讀

摘要:就是兩個(gè),一個(gè)有參數(shù),一個(gè)沒(méi)有,做法一樣。用或放入前綴,直到新的字符串長(zhǎng)度等于。

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.

Example
leftpad("foo", 5)
 "  foo"

leftpad("foobar", 6)
 "foobar"

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

就是兩個(gè)method,一個(gè)有參數(shù)padChar,一個(gè)沒(méi)有,做法一樣。用" "padChar放入originalStr前綴,直到新的字符串長(zhǎng)度等于size。

Solution
public 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();
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65804.html

相關(guān)文章

  • LintCode Coins in a line III

    摘要:復(fù)雜度思路參考的思路,對(duì)于,表示在從到的范圍內(nèi),先手玩家能拿到的最大的硬幣價(jià)值。對(duì)于狀態(tài),先手玩家有兩種選擇,要么拿的硬幣,要么拿的硬幣左邊一個(gè)的或者右邊一側(cè)的,如果拿左側(cè)的硬幣,如果拿右側(cè)的硬幣,取兩個(gè)值的最大值。 LintCode Coins in a line III There are n coins in a line. Two players take turns to ...

    focusj 評(píng)論0 收藏0
  • Lintcode Coins in a line II

    摘要:兩個(gè)參賽者輪流從左邊依次拿走或個(gè)硬幣,直到?jīng)]有硬幣為止。計(jì)算兩個(gè)人分別拿到的硬幣總價(jià)值,價(jià)值高的人獲勝。請(qǐng)判定第一個(gè)玩家是輸還是贏樣例給定數(shù)組返回給定數(shù)組返回復(fù)雜度思路考慮先手玩家在狀態(tài),表示在在第的硬幣的時(shí)候,這一位玩家能拿到的最高價(jià)值。 LintCode Coins in a line II 有 n 個(gè)不同價(jià)值的硬幣排成一條線(xiàn)。兩個(gè)參賽者輪流從左邊依次拿走 1 或 2 個(gè)硬幣,直...

    2shou 評(píng)論0 收藏0
  • PHP之string之str_pad()函數(shù)使用

    摘要:使用另一個(gè)字符串填充字符串為指定長(zhǎng)度該函數(shù)返回被從左端右端或者同時(shí)兩端被填充到制定長(zhǎng)度后的結(jié)果。如果的值是負(fù)數(shù),小于或者等于輸入字符串的長(zhǎng)度,不會(huì)發(fā)生任何填充,并會(huì)返回。如果填充字符的長(zhǎng)度不能被整除,那么可能會(huì)被縮短。 str_pad (PHP 4 >= 4.0.1, PHP 5, PHP 7) str_pad — Pad a string to a certain length w...

    qpwoeiru96 評(píng)論0 收藏0
  • [LintCode] Remove Node in Binary Search Tree [理解BS

    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...

    陳江龍 評(píng)論0 收藏0
  • [LintCode/LeetCode] Flatten Binary Tree to Linked

    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 ...

    TNFE 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

X1nFLY

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<