将一个数字分解为一组单个数字 [英] Break A Number Up To An Array of Individual Digits

查看:50
本文介绍了将一个数字分解为一组单个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有整数 123 并且我想将数字分解成一个数组 [1,2,3],那么最好的方法是什么?我已经搞砸了很多,我有以下工作:

If I have the integer 123 and I want to break the digits into an array [1,2,3] what is the best way of doing this? I have messed around with this a lot and I have the following working:

var number = 123    
var digits = Array(String(number)).map{Int(strtoul((String($0)),nil,16))}

我看着它,觉得可能有更好/更简单的方法来做到这一点.如果没有,那么它可能会出现在网络搜索中.任何替代想法?

I look at it and feel there might be an better/easier way of doing this. If not then maybe it will come up on web searches. Any alternatives ideas?

推荐答案

处理数字字符串的 UTF-8 表示更容易因为十进制数字的 UTF-8 编码单元可以很容易地转换为减去一个常数得到相应的整数:

It is easier to work on the UTF-8 representation of the number string because the UTF-8 code unit of a decimal digit can easily be converted to the corresponding integer by subtracting a constant:

let asciiZero = UInt8(ascii: "0")
let digits = map(String(number).utf8) { Int($0 - asciiZero) }

事实证明,这也明显更快.

This also turned out to be significantly faster.

如果性能是主要目标,那么您应该限制不使用字符串的简单整数运算方法或字符:

If performance is the primary goal then you should restrict the method to simple integer arithmetic, without using strings or characters:

var digits : [Int] = []
while number > 0 {
    digits.insert(number % 10, atIndex: 0)
    number /= 10
}

为了您的方便,这是我完整的测试代码(编译Xcode 6.4 在 MacBook Pro 上处于 Release 模式).

Here is my complete test code for your convenience (compiled with Xcode 6.4 in Release mode on a MacBook Pro).

func digits1(number : Int) -> [Int] {
    let digits = Array(String(number)).map{Int(strtoul((String($0)), nil, 16))}
    return digits
}

func digits2(number : Int) -> [Int] {
    // Use a static property so that the constant is initialized only once.
    struct Statics {
        static let asciiZero = UInt8(ascii: "0")
    }

    let digits = map(String(number).utf8) { Int($0 - Statics.asciiZero) }
    return digits
}

func digits3(var number : Int) -> [Int] {
    var digits : [Int] = []
    while number > 0 {
        digits.insert(number % 10, atIndex: 0)
        number /= 10
    }
    return digits
}

func measure(converter: (Int)-> [Int]) {
    let start = NSDate()
    for n in 1 ... 1_000_000 {
        let digits = converter(n)
    }
    let end = NSDate()
    println(end.timeIntervalSinceDate(start))
}

measure(digits1) // 10.5 s
measure(digits2) // 1.5 s
measure(digits3) // 0.9 s

<小时>

Swift 3 更新:

func digits(_ number: Int) -> [Int] {
    var number = number
    var digits: [Int] = []
    while number > 0 {
        digits.insert(number % 10, at: 0)
        number /= 10
    }
    return digits
}

print(digits(12345678)) // [1, 2, 3, 4, 5, 6, 7, 8]

事实证明这也比附加数字稍快到一个数组并在最后反转它.

This also turned out to be slightly faster than appending the digits to an array and reversing it at the end.

这篇关于将一个数字分解为一组单个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆