【Better Code】repeat

《JavaScript 框架设计》

版本1:

function repeat(target, n) {
    return (new Array(n + 1)).join(target)
}

版本2:

function repeat(target, n) {
    return Array.prototype.join.call({length: n + 1}, target)
}

版本3:(缓存)

var repeat = (function() {
    var join = Array.prototype.join,
        obj = {}
    return function(target, n) {
        obj.length = n + 1
        return join.call(obj, target)
    }
})();

版本4:(算法)

function repeat(target, n) {
    var s = target,
        total = []
    while (n > 0) {
        if (n % 2 == 1) total[total.length] = s
        if (n == 1) break
        s += s
        n = n >> 1
    }

    return total.join(‘‘)
}

版本5:

function repeat(target, n) {
    var s = target,
        c = s.length * n
    do {
        s += s
    } while (n = n >> 1)
    s = s.substring(0, c)
    return s
}

版本6:

function repeat(target, n) {
    var s = target
        total = ‘‘
    while (n > 0) {
        if (n % 2 == 1) total += s
        if (n == 1) break
        s += s
        n = n >> 1
    }
    return total
}

版本7:(递归)

function repeat(target, n) {
    if (n == 1) {
        return target
    }
    var s = repeat(target, Math.floor(n / 2))
    s += s
    if (n % 2) {
        s += target
    }
    return s
}
时间: 2024-08-25 18:16:53

【Better Code】repeat的相关文章

【Leet Code】Palindrome Number

Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an integer is a palindrome. Do this without extra space. 判断一个数整数是不是回文?例如121,1221就是回文,好吧,直接利用前面写过的[Leet Code]Reverse Integer--"%"你真的懂吗? 不过这里要考虑翻转后,数值

【Leet Code】Median of Two Sorted Arrays

Median of Two Sorted Arrays Total Accepted: 17932 Total Submissions: 103927My Submissions There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n

【Leet Code】ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSII

【Leet Code】Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters Total Accepted: 20506 Total Submissions: 92223My Submissions Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating le

【Gray Code】cpp

题目: The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0

【Leet Code】Add Two Numbers

Add Two Numbers Total Accepted: 20255 Total Submissions: 88115My Submissions You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numb

【Leet Code】Reverse Integer——“%”你真的懂吗?

Reverse Integer Total Accepted: 27372 Total Submissions: 68133My Submissions Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 题目咋看起来很简单,其实,真的很简单: class Solution { public: int reverse(int x) { int ret = 0; bo

【Leet Code】String to Integer (atoi) ——常考类型题

String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yours

【Better Code】jQuery

$('#id1, #id2').html('')