[Cattle off network - offer the lowest number wins the rotating array]

topic:

The beginning of an array of several elements moved to the end of the array, the array we call rotation.
A non-descending order of the input array of a rotation of the output rotary smallest element array.
For example, an array {3,4,5,1,2} {1,2,3,4,5} is a rotation of the array to a minimum.
NOTE: All the elements are given in greater than 0, if the array size is 0, return 0.

Knowledge and concepts:

a. cattle passenger network JavaScript (V8) Input Output Operations Guide

. B the JavaScript regex pipe character "|" (vertical resolution) Detailed
c. using the maximum and minimum demand Math.max array and Math.min

Ideas:

The idea is very simple, is to use an array of Math.min to find out the minimum

Code:

 while (line = readline()){
    let arr = line.replace(/\[|\]/g,'').split(',');
    console.log(Math.min.apply(null,arr));
 }

In vscode in their own test cases:

var a='[3,4,5,1,2]';  //readline()读取的字符串
let b = a.replace(/\[|\]/g,'');  //去掉中括号
console.log(b);
var arr=b.split(','); //以逗号分开,放入数组
console.log(arr);
console.log(Math.min(...arr));  //求最小值
console.log(Math.max.apply(null,arr));  //求最大值

Test Results:

Guess you like

Origin www.cnblogs.com/xiakecp/p/11580652.html