Swift Solution with Quick Sort func


  • 0
    Z
    class Solution {
        func arrayPairSum(_ nums: [Int]) -> Int {
            var SortedArray = quickSort(data: nums)
    		var number = 0
    		for var i in 0..<SortedArray.count {
    			if i % 2 != 0 {
    				number += 0
    			} else {
    				number += SortedArray[i]	
    			}
    		}
    		return number
        }
    }
    func quickSort(data: [Int]) -> [Int] {
    	if data.count <= 1 {
    		return data
    	}
    	var left: [Int] = []
    	var right: [Int] = []
    	let pivot: Int = data[data.count - 1]
    	for index in 0..<data.count-1 {
    		if data[index] < pivot {
    			left.append(data[index])
    		} else {
    			right.append(data[index])
    		}
    	}
    	var result = quickSort(data: left)
    	result.append(pivot)
    	let rightResult = quickSort(data: right)
    	result.append(contentsOf: rightResult)
    	return result
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.