我正在编写一个算法来查找数组中的最小数字但是我的print语句一直说最低的数字是0.我有以下内容:
var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
func findMin(numbers: NSArray) {
var minValue = numbers[0]
var isSmallest: Bool
for i in 0...numbers.count {
isSmallest = true
for j in 0...numbers.count {
if i > j {
isSmallest = false
}
}
if isSmallest {
minValue = i
}
}
print("Smallest value in the list is \(minValue)")
}
findMin(numbers: list as NSArray)
我的print语句返回:
"Smallest value in the list is 0\n"
我觉得算法是正确的。有什么想法吗?
编辑:回答了我自己的问题
我正在迭代索引而不是实际值。感谢评论中的一位用户。正确的代码应该是:
var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
func findMin(numbers: NSArray) {
var minValue = numbers[0]
var isSmallest: Bool
for i in list {
isSmallest = true
for j in list {
if i > j {
isSmallest = false
}
}
if isSmallest {
minValue = i
}
}
print("Smallest value in the list is \(minValue)")
}
findMin(numbers: list as NSArray)
答案 0 :(得分:10)
简单地
let list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
let minValue = list.min()
答案 1 :(得分:1)
或者你可以使用
var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
list.min() // returns 2
答案 2 :(得分:1)
对于逻辑用途,试试这个
var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
var minValue = list[0]
for num in list {
minValue = (num < minValue) ? num : minValue
}
print("Smallest value in the list is \(minValue)")
按属性直接获取最小值
let list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
let minValue = list.min()
答案 3 :(得分:0)
这是你的解决方案
let numbers = [1, 6, 3, 9, 4, 6]
let min = minElement(numbers) // 1
let position = find(array, min)// it will return index
答案 4 :(得分:0)
如果您想在没有额外循环的情况下找到最小值,请尝试以下方法:
var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
func findMin(numbers: NSArray) {
var minValIdx = 0
var minValue = numbers[0] as! Int
for i in 1..<numbers.count {
if (numbers[i] as! Int) < minValue {
minValue = numbers[i] as! Int
minValIdx = i
}
}
print("Smallest value in the list is \(minValue)")
}
findMin(numbers: list as NSArray)
答案 5 :(得分:0)
假设您必须实际显示某些逻辑,那么就抛出更多选项:
func min<T:Comparable>(_ elements:[T]) -> T? {
guard let first = elements[0] else {
return nil
}
return elements.reduce(first, min)
}
print(min(list))
或将其放在扩展名中,这基本上是Array.min
extension Array where Element : Comparable {
func smallest() -> Element? {
guard let first = self.first else {
return nil
}
// Use 'Swift.min' to get to the global function since Array
// already has a min function
return reduce(first, Swift.min)
}
}
print(list.smallest())
答案 6 :(得分:0)
您可以使用此代码: 它在C#
var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
int minVal = list[0];
for (int i = 1; i < list.Length; i++)
{
if (list[i] < minVal)
{
minVal = intArray[i];
}
}
答案 7 :(得分:0)
要查找序列中的最小元素, Swift 3 有一个称为min()
的istance方法:
var list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
let minElem = list.min()
如果序列无元素,则返回 nil 。
此方法也可用于浮点列表:
let heights = [67.5, 65.7, 64.3, 61.1, 58.5, 60.3, 64.9]
let minHeight = heights.min()
您可以找到官方文件参考here
答案 8 :(得分:0)
您可以在Swift中将此代码用于手动算法:
let list = [5, 4, 3, 5, 2, 50, 8, 10, 300]
var smallest = list[0]
for item in list {
if (item < smallest) {
smallest = item
}
}
print("smallest number is: \(smallest)")
如果您想让Swift做艰苦的工作,请使用以下代码:
let smallest = list.min()
print("smallest number is: \(smallest)")