This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static double mean(List<Integer> nums) { | |
int sum = 0; | |
for (int i = 0; i < nums.size(); i++) { | |
sum += nums.get(i); | |
} | |
return (double) sum / nums.size(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static double mean(List<Integer> nums) { | |
if (nums == null || nums.size() == 0) { | |
return 0.0; | |
} | |
int sum = 0; | |
for (int i = 0; i < nums.size(); i++) { | |
sum += nums.get(i); | |
} | |
return (double) sum / nums.size(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static double mean(List<Integer> nums) { | |
if (nums == null || nums.size() == 0) { | |
return 0.0; | |
} | |
double sum = 0.0; | |
for (int i = 0; i < nums.size(); i++) { | |
sum += nums.get(i); | |
} | |
return sum / nums.size(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static double mean(List<Integer> nums) { | |
if (nums == null || nums.size() == 0) { | |
return 0.0; | |
} | |
double sum = 0.0; | |
for (Integer n : nums) { | |
sum += n; | |
} | |
return sum / nums.size(); | |
} |
La lección que espero transmitir con esta entrada es que incluso "algoritmos" relativamente sencillos como la media aritmética pueden dar mucho juego cuando uno se para a pensar en las diferentes optimizaciones aplicables.
No hay comentarios:
Publicar un comentario