Last active
April 18, 2023 03:36
-
-
Save harunorimurata/205b2f83278b2d77f308d500c387b2a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func Repeat(s string, n uint) string { | |
| switch n { | |
| case 0: | |
| return "" | |
| case 1: | |
| return s | |
| } | |
| max := len(s) * int(n) | |
| var b strings.Builder | |
| b.Grow(max) | |
| b.WriteString(s) | |
| for ; 2 <= n; n = n >> 1 { | |
| b.WriteString(b.String()) | |
| } | |
| b.WriteString(b.String()[:max-b.Len()]) | |
| return b.String() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const repeat = (s, n) => { | |
| if (n === 0) return ''; | |
| else if (n === 1) return s; | |
| const max = s.length * n; | |
| for (; 2 <= n; n = n >> 1) { s += s; } | |
| return s + s.substring(0, max - s.length); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment