Open yygmind opened 5 years ago
`function convert(num){ if(!num){ return ''; } return num % 10 + convert((num / 10) | 0); }
var int = 12345; console.log(typeof convert(int));`
let a = 2341
function b(num) {
return num.toString().split('').sort((x,y) => {return y - x}).join('')
}
let str = b(a)
console.log(str)
function rotate(nums) { let str = '' let current = nums%10 str = str+current if (Math.floor(nums/10) > 0 ) { return str+ rotate(Math.floor(nums/10)) } else { return str }
}
function f(num) {
let a = num%10
if(num<10) return num+''
return a + '' + f((num-a)/10)
}
function reverse(n) {
return n<0?'-'+reverse(Math.abs(n)):((n<10)?String(n): (n%10+''+reverse(Math.floor(n/10))));
}
function test(num){ num=num.toString(); if(num.length==1){ return num }else{ return num.substring(num.length-1)+test(num.substring(0,num.length-1)) } }
function print(num) {
let yu = num%10;
let shang = (num-yu)/10;
return shang ? ${yu}${print2(shang)}
: yu;
}
function sub(str){
let out
if(str.length>1){
out=sub(str.substring(1))+str[0]
}else{
return str
}
return out
}
function f2(number){
const str = number.toString();
if(number < 10){
return number.toString()
}else {
return str.slice(str.length-1, str.length)+ f2(str.slice(0, str.length-1));
}
}
考虑转化规则为: 1000逆序结果为'1' 而不是'0001' -1000逆序结果为'-1' 而不是'-0001'
function calc(num) {
if (num < 0) {
return `-${+calc(Math.abs(num) + '')}`;
}
if (num < 10) {
return num;
}
let n = Math.floor(num / 10);
let m = num % 10;
let ans = `${m}${calc(n + '')}`;
return (typeof num === 'string') ? ans : `${+ans}`;
}
function helper(number) {
if (number === 0) return "";
return String(number % 10) + helper((number / 10) >> 0);
}
function reverseNumber(number) {
if (number === 0) return "0";
if (number < 0) return String(`-${helper(-1 * number)}`);
return helper(number);
}
如果大家对算法感兴趣可以关注我的leetcode题解
用 JavaScript 写一个函数,输入 int 型,返回整数逆序后的字符串。如:输入整型 1234,返回字符串“4321”。要求必须使用递归函数调用,不能用全局变量,输入函数必须只有一个参数传入,必须返回字符串。
function reverse(data) { let arr = []; let val = data % 10; arr.push(val); if (val) { return arr.concat(reverse(parseInt(data / 10))).join(""); }; }
这样算不算玩文字游戏(😁
const convert = (number) => {
let num = number
while (num % 10 === 0) {
num = num / 10
}
const _reverse = (str) => {
return str.length <= 0 ? '' :`${_reverse(str.slice(1))}${str.slice(0,1)}`
}
return _reverse(`${num}`)
}
convert(1234)
convert(1230)
完了,难到我了,不能用爱发电了
function reverse(num) {
const str = num.toString();
if(str.length === 1) return str; // 如果切分成一个数就返回
const lastNum = str.slice(str.length -1); // 获取最后一个数
return lastNum + reverse(str.slice(0, str.length -1)); // 递归相加
}
入参数判断一下类型
const reverseNum = (num) => {
//let str_arr = num.toString().split('')
if (typeof num === 'number') {
return reverseNum(num.toString().split('').reverse())
} else if (Array.isArray(num)){
return num.join('')
} else {
throw '输入需为int型整数'
}
}
function reverse(nums){
const str = nums.toString()
if(str.length===0){
return str
}
return str.slice(-1) + reverse(str.slice(0,-1))
}
console.log(reverse(123456789))
function reverse(num) {
if(num.toString().length === 1){
return num
}
return reverse(num.toString().slice(1)) + num.toString()[0];
}
function convert(number) {
let s = number % 10
let num = parseInt(number / 10)
if(num <= 0) {
return s
}
return `${s}${convert(num)}`
}
convert(1234)
function convert(str) {
str = (str).toString()
if (str)
return convert(str.substr(1)) + str.substr(0, 1)
return ''
}
convert(12345)
const numToReverseStr = (num) => {
let result = num % 10;
if (num === 0) {
return ''
}
const newNum = Number(String(num).slice(0 ,-1));
return String(result) + numToReverseStr(newNum);
}
function getRerviseString(num) {
if (num < 10) return num;
return (num % 10) + '' + getRerviseString(((num - (num % 10)) / 10))
}
console.log(getRerviseString(4576567567))
考递归相关的知识
function reverse(num) {
return String(num).split('').reverse().join('');
}
function reverse2(num) {
if (num < 10) {
return String(num);
} else {
return String(num % 10) + reverse2(Math.floor(num / 10));
}
}
function reverse3(num) {
let result = '';
while(num) {
result += String(num % 10);
num = Math.floor(num / 10);
}
return result;
}
console.log(reverse(1234567));
console.log(reverse2(1234567));
console.log(reverse3(1234567));
function fun(num){ let num1 = num / 10; let num2 = num % 10; if(num1<1){ return num; }else{ num1 = Math.floor(num1) return `${num2}${fun(num1)}` } } var a = fun(12345) console.log(a) console.log(typeof a)
let intToStr = function(num) {
let num1 = num / 10;
let num2 = num % 10;
if (num1 < 1) {
return ${num}
} else {
num1 = Math.floor(num1);
return ${num2}${intToStr(num1)}
}
}
let result = intToStr(1234);
console.log(result, typeof result);
!function(num){
let span='';
while(Math.floor(num)>=1){
const temp = num%10;
num/=10;
span+=temp|0;
}
console.log(span)//4321
}(num=1234)
/**
* 用 JavaScript 写一个函数,输入 int 型,返回整数逆序后的字符串。如:输入整型 1234,返回字符串“4321”。要求必须使用递归函数调用,不能用全局变量,输入函数必须只有一个参数传入,必须返回字符串。
* @param {Number} num
* @returns {String}
*/
function reverse(num) {
let arr = Number.isInteger(num) ? ['', num.toString()] : num;
if (arr[1].length > 0) {
let copyArr = [...arr[1]];
return reverse([(arr[0] += copyArr.pop()), copyArr.join('')]);
}
return arr[0];
}
console.log(reverse(1234));
function reverse(num) {
if (num) return reverse(num.toString().substring(1)) + num.toString().substring(0, 1);
return '';
}
console.log(reverse(1234));
function ints(num) { var str = String(num),i=str.length; if(i>1) { var newNum = Number(str.substring(0,i-1)); var d = str.substring(i-1); return d+ints(newNum); }else { return num; } } ints(1234);
// 当前仅考虑非负整数
function intReverse(num) {
if(!/^[1-9]\d*|0$/.test(num)) {throw new Error('请传入非负整数')};
strNumber = num + '';
if(strNumber.length === 1) {return strNumber};
let tail = strNumber.slice(-1);
let rest = parseInt(strNumber.slice(0,-1));
return tail + intReverse(rest)
}
function test(num){ num = typeof num === 'number' ? num.toString() : num if(num.length <= 1) return num else return num[num.length - 1] + test(num.substring(0, num.length - 1)) }
test的输入必须为整数,输出必须为字符串
function intToStringReverse(num){
if(num <= 0) return ""
return "" + num % 10 + intToStringReverse(parseInt(num / 10))
}
如果能传两个参数的话,这个算法将会更好。 尾递归调用。性能上好不少。
function func(v, r = '') { v += '' if (v.length < 1) return r r += v.slice(v.length-1) v = v.slice(0, v.length-1) return func(v, r) } func(1234) // "4321"
function func(v) {
var r = [...arguments][1] || ''
v += ''
if (v.length < 1) return r * 1
r += v.slice(v.length-1)
v = v.slice(0, v.length-1)
return func(v, r)
}
在您代码上加了俩点:1、省去第二个参数,2、输出数字格式
function reverse(num) { const input = new String(num)
if (input.length <= 1) return input;
return input[input.length - 1] + reverse(input.slice(1, input.length - 1)) + input[0]; }
function bilibili(num) {
if (num.toString().length < 2) {
return num.toString()
}
let a = Math.floor(num / 10)
let b = num % 10
return ${b}${bilibili(a)}
}
function fun(num){ let num1 = num / 10; let num2 = num % 10; if(num1<1){ return num; }else{ num1 = Math.floor(num1) return `${num2}${fun(num1)}` } } var a = fun(12345) console.log(a) console.log(typeof a)
思路也太巧妙了
function reverse(num) {
if (!num) return '';
return '' + num % 10 + reverse(parseInt(num / 10));
}
const num = 1234;
function func(number) {
return number > 9 ? String(number % 10) + func(Math.floor(number / 10)) : String(number);
}
console.log(func(1234), typeof func(1234))
` function reverseInt(int) { if(!Number.isInteger(int) || arguments.length !== 1) { return new Error('only one params was required and must be a interger number') } int += '' const cut = int.slice(-1) if(int.length == 1) { return int } else { int = int.substr(0,int.length - 1) return cut + reverseInt(int) }
} console.log(typeof reverseInt(23645583)) // 'string' console.log(reverseInt(23645583)) // 38556432 console.log(reverseInt(23645583.123)) // 报错 console.log(reverseInt(123,23645583)) // 报错 `
function formatNumber(num) {
let str = num.toString();
const length = str.length;
let value = '';
if (length > 1) {
let subStr = str.substr(0, length - 1)
let next = parseInt(subStr)
value = str.substr(length - 1, 1) + formatNumber(next)
} else {
value = str.substr(length - 1, 1)
}
return value
}
let value = formatNumber(2020)
console.log(value)
// "0202"
function reserve(num) {
let str = num.toString();
let len = str.length
let first = str[len - 1]
if (str.length > 1) {
return first + reserve(str.substring(0, len-1))
} else {
return first
}
}
console.log(reserve(423456777));
console.log(typeof(reserve(423456777)));
function reverseInt(num) {
return num < 10
? num
: ${num % 10}${reverseInt(parseInt(num / 10))}
}
这样它不香么
香但是差一点。必须返回字符串。reverseInt(1) number
function reverseInt(num) {
if(num < 10) return num+'';
return ${num % 10}${reverseInt(parseInt(num / 10))}
}
------------------ 原始邮件 ------------------ 发件人: "fasign"<notifications@github.com>; 发送时间: 2020年1月17日(星期五) 上午10:29 收件人: "Advanced-Frontend/Daily-Interview-Question"<Daily-Interview-Question@noreply.github.com>; 抄送: "杭州电子科技大学-胡大昌"<707555326@qq.com>; "Comment"<comment@noreply.github.com>; 主题: Re: [Advanced-Frontend/Daily-Interview-Question] 第 99 题:编程算法题 (#153)
function reverseInt(num) { if(num < 10) return num return ${num % 10}${reverseInt(parseInt(num / 10))} }
这样它不香么
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.
function r (n) {
if (n.length <= 1) {
return n
}
const first = n[0]
const rest = n.slice(1)
return r(rest) + first
}
function reverse(num) {
if (num===0) return '';
return parseInt(num%10) + '' + reverse(parseInt(num/10))
}
function outerFn(num){ var count=0 var str=num+"" var l=str.length var result="" function innerFn(){ if(count==l) return console.log(result) result+=str.slice(-count-1)[0] count++ innerFn() } return innerFn() } outerFn(31234)//43213
负数+尾递归
function reverseNumber(number) {
const flag = number < 0 ? -1 : 1;
number = Math.abs(number);
const pre = [...arguments][1] || '';
if (number < 10) {
return pre + String(number);
}
return reverseNumber(Math.floor(number / 10), pre + flag * (number % 10));
}
代码 如下,支持负数
function reverseNum(num) {
if(num < 0) return '-' + reverseNum(Math.abs(num));
let str = num.toString();
return str.length === 1 ? str : str.slice(-1) + reverseNum(str.slice(0,-1));
}
function reverse(num, i = 0, acc = '') {
if (typeof num == 'number') {
num += '';
i = num.length - 1;
}
if (i < 0) {
return acc;
}
acc += num[i];
return reverse(num.substring(0, i--), i, acc);
}
reverse(1234); // 4321
// 符合ES6尾调用优化规则
function reverse(num) {
if(num > 10) {
let a = num / 10
let b = num % 10
return b + reverse((num - b) / 10)
}else if(num < 0) {
return reverse(-num) + '-'
}else {
return num + ''
}
}
function reverseNum(num){
if(num<10){
return String(num)
}
let min1 = Math.floor(num/10);
let min2 = num%10;
if(min1<10){
return `${min2}${min1}`;
}
return `${min2}${reverseNum(min1)}`
}
用 JavaScript 写一个函数,输入 int 型,返回整数逆序后的字符串。如:输入整型 1234,返回字符串“4321”。要求必须使用递归函数调用,不能用全局变量,输入函数必须只有一个参数传入,必须返回字符串。