摘要:就是兩個(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.
Exampleleftpad("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。
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(); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65804.html
摘要:復(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 ...
摘要:兩個(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è)硬幣,直...
摘要:使用另一個(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...
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 ...
閱讀 1483·2019-08-30 15:55
閱讀 1187·2019-08-30 15:52
閱讀 1304·2019-08-29 13:53
閱讀 1477·2019-08-29 11:19
閱讀 2987·2019-08-26 13:29
閱讀 540·2019-08-26 11:33
閱讀 2605·2019-08-23 17:20
閱讀 1035·2019-08-23 14:14