-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
第 99 题:编程算法题 #153
Comments
|
其实这种直接使用JavaScript的API更简单直观。 // the first solution by recursion
const numToReverseStr = num => {
if( 'number' !== typeof num ) throw '输入需为int型整数';
if(!Math.floor(num / 10)) return num.toString();
return (num % 10).toString() + numToReverseStr( Math.floor(num / 10) );
}
console.log(numToReverseStr(2169362));
// the second solution not by recursion but JavaScript API
const numToReverseStr_0 = num => {
return num.toString().split('').reverse().join('');
}
console.log(numToReverseStr_0(2169362)); |
`function test(num) { } var s = test(1234); console.log(s);` |
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))
} |
这样行不,有错望指出 |
function reverseNumber (num) { |
function out(value){
if(value.length===0) return '';
value = Array.isArray(value)?value:value.toString().split('');
return value.pop() + out(value);
} |
|
`var a = 1234; function reverse(a){ reverse(a)` |
思路:先将传入的参数转为string,取该字符串的第二位至末尾作为下一次递归函数的参数,判断当前字符串长度是否大于1,大于1则递归调用,等于1就返回这个字符串 function myReverse(num){
let str = num.toString()
let num2 = parseInt(str.slice(1))
if(str.length >1){
return myReverse(num2) + str[0]
}else {
return str
}
}
console.log(myReverse(1234)) |
function reverse(num) {
const str = `${num}`
return str ? `${str % 10}${reverse(str.slice(0, str.length - 1))}` : ''
} |
function renum(num) {
num = num.toString()
let rest = num.slice(0, -1),
last = num.slice(-1);
return last.length ? last + renum(rest) : ''
}
console.log(renum(12345678)); // 87654321 |
如果能传两个参数的话,这个算法将会更好。 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" |
科里化一下就好了,在外面封装一层函数只接收参数 |
输入的数字小于 10 的时候返回的是整形 |
const convert = num =>
(num / 10) | 0
? String(num % 10) + convert((num / 10) | 0 )
: String(num)
let res = convert(12345)
console.log(res) // 54321
console.log(typeof res)// string |
const calc = nums => nums.toString().split('').reduce((sum, num) => num + sum); |
贴上两种方法
|
function intToString (int) {
if (int >= 10) {
return String(int % 10) + intToString(Math.floor(int / 10))
}
return String(int)
} |
function doo (num) {
const str = `${num}`;
const temp = str.length > 2 ? doo(str.slice(1)) : str.slice(1);
return `${temp}${str[0]}`
} 先转成字符串,然后根据长度递归调用 |
参考了各位大佬的思路,用%10来取最后一位,用/10来减少位数,用>>0来去除小数点,最后用es6的模板字符串来返回字符串类型的结果 |
int型整数也包含负数,发现大多数解法(包括我的)都只考虑传入数值为正数的情况,题干中也没有相关示例,求解 @yygmind |
function fn1(num){
var str = num.toString();
let i = 0,newStr="";
while(i<str.length){
newStr+=str.slice(str.length-(i+1),str.length-i);
i++
}
return newStr;
}
fn1(1234) |
var str = '12345'; |
我觉着这个还是得加一些异常case,比如1000,或者负数 /**
* @case 1234 => '4321'
* @case 1000 => '1'
* @case -1000 => '-1'
* @case -1234 => '-4321'
*
* @param {number} number 传入的数值
*/
function solution(number) {
if (number < 0) {
return `-${solution(Math.abs(number))}`;
}
if (number < 10) {
return `${number}`;
}
return `${number % 10 || ''}${solution(~~(number / 10))}`;
}
const assert = require('assert').strict;
assert.strictEqual(solution(1234), '4321');
assert.strictEqual(solution(1000), '1');
assert.strictEqual(solution(-1234), '-4321');
assert.strictEqual(solution(-1000), '-1'); |
function reverseNum(x){
if(x<0){
return '-'+reverseNum(Math.abs(x))
}else if(typeof x === 'number'){
return reverseNum(String(x))
}
return x.length===0?x:reverseNum(x.slice(1)) + x[0]
}
reverseNum(1234560)
reverseNum(-1234560) |
|
|
|
function reverse(num) {
// 得到个位数
var mod = num % 10
// 剩余位数
var n = Math.floor(num / 10)
if(n <= 0) {
return num
} else {
return '' + mod + reverse(n)
}
} |
function toreverseString(int){
if(!int&&int!==0) return ''
var arr=int.toString().split('');
var str = arr.pop()
return str+toreverseString(arr.join(''))
} |
小于10的时候toString()一下么 |
function core(input) {
input = (input + "")
let length = input.length
let count = length - 1
let result = ""
return function f1() {
result += input[count]
if (count <= 0) {
count = length - 1
return result
}
count--
return f1.call(null)
}
}
core(123456789)() // 987654321 |
这可能是不依赖任何API的最佳实践了吧 |
// 用递归。第一步 处理递归到底的条件 第二步 构建一个更小问题的解。 第三步 用更小问题的解构建出原问题的解 |
function numberToString(num) { |
function numToString(num, res = []) {
if (num == '') return;
let temp = Number(num) % 10;
res.push(temp);
let str = num.toString();
if (res.length !== 0) {
numToString(str.slice(0, -1), res)
}
return res.join('');
} |
|
function reverse(val) { |
function reverse(num){
var str=String(num)
if(str.length===1){
return str
}
return reverse(str.slice(-1))+reverse(str.slice(0,-1))
} |
function fun(int, x = int.toString().split(""), y = "") {
if (x.length == 0) return y;
else y = fun(undefined, x, x.shift()) + y;
return y;
}
console.time("my");
console.log(fun(123212312312312313212312123321231231231));
console.timeEnd("my");
console.time("sys");
console.log(
(123212312312312313212312123321231231231)
.toString()
.split("")
.reverse()
.join("")
);
console.timeEnd("sys"); |
function getRevString(num){ } |
function reverseN2S(num) {
let str = num + '';
let index = str.length - 1;
return str[index] + (index > 0 ? reverseN2S(str.substring(0, index)) : '');
}
console.log(reverseN2S(1234)); // '4321'
console.log(reverseN2S(-1000)); // '0001-' |
|
function reverse(num) {
if(num == undefined) {
return
}
var str = num + ''
if(str.length == 0) {
return str
}
var s = str.charAt(str.length - 1)
return s + reverse(str.substr(0, str.length - 1))
} |
function reverse(num) {
num = typeof num === 'number' ? '' + num : num;
if (!num.length) return '';
return num.slice(-1) + reverse(num.slice(0, -1));
} |
|
function solve(int) {
if (int === 0) return '';
const tar = int % 10;
// 如果可以不可以用Math
// return tar.toString() + solve(int / 10 - int / 10 % 1);
// 如果可以用parseInt
return tar.toString() + solve(parseInt(int / 10));
} |
代码:const print = (num) => num < 10 ? '' + num : num % 10 + print(parseInt(num / 10)) |
|
|
|
const reverseToString = (value, res = []) => {
if (value > 0) {
const digital = value % 10;
res.push(digital);
return reverseToString(Math.floor(value / 10), res);
}
return res.join('');
}
const res = reverseToString(1234);
console.log(res); |
|
function func(num) {
const str = num + '';
if (str.length === 1) {
return str;
}
const newStr = str.slice(-1);
const remainingStr = str.slice(0, -1);
return newStr + func(remainingStr);
} |
(1234).toString().split('').reverse().join('') |
const reverse = (num) => {
let res = 0;
while (num) {
res = res * 10 + (num % 10)
num = ~~(num / 10) // 取整数
}
return res + ''; // 最后强转 String 类型
};
reverse(12345) // '54321' |
用 JavaScript 写一个函数,输入 int 型,返回整数逆序后的字符串。如:输入整型 1234,返回字符串“4321”。要求必须使用递归函数调用,不能用全局变量,输入函数必须只有一个参数传入,必须返回字符串。
The text was updated successfully, but these errors were encountered: