Description:
Given few numbers, you need to print out the digits that are not being used.
Example:
unusedDigits(12, 34, 56, 78) // "09"
unusedDigits(2015, 8, 26) // "3479"
Note:
Result string should be sorted
The test case won't pass Integer with leading zero
我的解法:
function unusedDigits() {
var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var arguArray = arguments;
var s = "";
for (var i = 0; i < arguArray.length; i++) {
var lalla = arguArray[i].toString();
s += lalla;
}
for (var j = 0; j < s.length; j++) {
for (var k = 0; k < arr.length; k++) {
if (s.charAt(j) == arr[k].toString()) {
arr.splice(k, 1);
}
}
}
return arr.join("");
}
unusedDigits(12, 34, 56, 78);
大牛的解法:
function unusedDigits(...args){ return "0123456789".replace(new RegExp('['+args.join('')+']','g'), '')}
比较实际的写法:
function unusedDigits() {
return [].reduce.call(arguments, function (left, num) {
(num + '').split('').map(function (digit) {
left = left.replace(digit, '');
});
return left;
}, '0123456789');
}