Skip to content

Instantly share code, notes, and snippets.

@leohxj
Created June 11, 2014 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leohxj/fe2f0c38686d4940c344 to your computer and use it in GitHub Desktop.
Save leohxj/fe2f0c38686d4940c344 to your computer and use it in GitHub Desktop.
给定一个非空的JavaScript数字数组,找到最小值的索引。(如果最小值出现不止一次,那么任何此类索引是可以接受的。) 方式一,手工方式最快
// 1
function indexOfSmallest(a) {
var lowest = 0;
for (var i = 1; i < a.length; i++) {
if (a[i] < a[lowest]) lowest = i;
}
return lowest;
}
// 2
function indexOfSmallest(a) {
return a.reduce(function(lowest, next, index) {
return next < a[lowest] : index ? lowest; },
0);
}
// 3
function indexOfSmallest(a) {
return a.indexOf(Math.min.apply(Math, a));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment