返回结果-1
const str = "2 11 1";
const str1 = "8 07 1";
const str2 = "16 af ff";
function solution(str) {const [n, minuend, subtrahend] = str.split(" ");if (n < 2 || n > 35) return -1;else if (isValid(minuend) && isValid(subtrahend)) {const result = Math.abs(parseInt(minuend, n) - parseInt(subtrahend, n)).toString(n);const mark = parseInt(minuend, n) > parseInt(subtrahend, n) ? 0 : 1;return `${mark} ${result}`;} else {return -1;}function isValid(str) {const reg = /[^0-9a-z]/;if (reg.test(str)) return false;if (str.length > 100) return false;if (str.startsWith("0")) {return str === "0";}// 数字长度不能超过n,列如2机制不能超过2,16进制不能超过ffor (let i = 0; i < str.length; i++) {const char = str.charAt(i);if (/^[0-9]$/.test(char)) {if (parseInt(n) - parseInt(char) <= 0) return false;} else if (/^[a-z]$/.test(char)) {// 以16进制为例 a对应97 f对应102 需要满足 charCode-97 < n(16) -10if (String(char).charCodeAt() - 87 >= n) return false;}return true;}}
}console.log(solution(str2));
/*
2 11 1= > 0 10
8 07 1 => -1
6 af ff => 1 50 ( 预期是负数的16进制 50) 符合预期~*/