Swift-第K个数字

2017-05-29  本文已影响26人  FlyElephant

题目:有些数的素因子只有3,5,7.请设计一个算法,找出其中第k个数.

解法一

k个数字一定是有第k-1个数字与3,5,7之间乘积所得三个数字中的一个.
<pre><code>` func getKthFactorNumber(k:Int) -> Int {

    if k < 0 {
        return 0
    }
    
    var val:Int = 1
    var arr:[Int] = []
    addProducts(arr: &arr, num: 1)
    
    for _ in 0..<k {
        val = removeMin(arr: &arr)
        addProducts(arr: &arr, num: val)
    }
    return val
    
}

func removeMin(arr:inout [Int]) -> Int {
    if arr.count == 0 {
        return 1
    }
    var min:Int = arr.first!
    for num in arr {
        if min > num {
            min = num
        }
    }
    
    while arr.contains(min) {
       
        for i in 0..<arr.count {
            if arr[i] == min {
                arr.remove(at: i)
                break
            }
        }
    }
    return min
}

func addProducts(arr:inout [Int],num:Int) {
    arr.append(3 * num)
    arr.append(5 * num)
    arr.append(7 * num)
}`</code></pre>

解法二

解法一中会有大量的重复数字,反复被添加,改进如下:
<pre><code>` func getKthFactorNumber2(k:Int) -> Int {

    if k < 0 {
        return 0
    }
    
    var val:Int = 0
    var arr3:[Int] = [1]
    var arr5:[Int] = []
    var arr7:[Int] = []
    
    for _ in 0...k {
        
        let num3:Int = arr3.count > 0 ? arr3.first! : Int.max
        let num5:Int = arr5.count > 0 ? arr5.first! : Int.max
        let num7:Int = arr7.count > 0 ? arr7.first! : Int.max
        
        val = min(num3, min(num5, num7))
        
        if num3 == val {
            arr3.removeFirst()
            arr3.append(val * 3)
            arr5.append(val * 5)
        } else if num5 == val {
            arr5.removeFirst()
            arr5.append(val * 5)
        } else if num7 == val {
            arr7.removeFirst()
        }
        arr7.append(val * 7)
    }
    
    return val
}`</code></pre>

解法三

解法二通过三个数组分别存储3的倍数,5的倍数,7的倍数,可以在同一个数字中通过三个索引来解决.
<pre><code>` func getKthFactorNumber3(k:Int) -> Int {

    if k < 0 {
        return 0
    }
    
    var index3:Int = 0
    var index5:Int = 0
    var index7:Int = 0
    var arr:[Int] = [1]
    var val:Int = 0
    
    for _ in 0...k {
        
        val = min(arr[index3] * 3, min(arr[index5] * 5, arr[index7] * 7))
        
        if arr[index3] * 3 == val {
            index3 += 1
        }
        
        if arr[index5] * 5 == val {
            index5 += 1
        }
        
        if arr[index7] * 7 == val {
            index7 += 1
        }
        
        arr.append(val)
    }
    
    return arr[arr.count - 2]
}`</code></pre>

测试代码:
<pre><code>`var factor:Factor = Factor()
var factorNum:Int = factor.getKthFactorNumber(k: 10)
print("FlyElephant---3,5,7因子数字:(factorNum)")

var factorNum2:Int = factor.getKthFactorNumber2(k: 10)
print("FlyElephant---3,5,7因子数字:(factorNum2)")

var factorNum3:Int = factor.getKthFactorNumber3(k: 10)
print("FlyElephant---3,5,7因子数字:(factorNum3)")`</code></pre>

FlyElephant.png
上一篇 下一篇

猜你喜欢

热点阅读