一些递归的练习题
Posted GoldenaArcher
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一些递归的练习题相关的知识,希望对你有一定的参考价值。
一些递归的练习题
每道题前面大概会留个十来行空行所以答案会被折叠这样……?
-
数字和
/** */ // find the sum of digits of a positive integer number using recursion // 35 => 3 + 5 const sumOfDigits = (num: number) => if (num < 0) throw new Error('input should be a positive integer'); let sum = 0; if (num === 0) return 0; sum += (num % 10) + sumOfDigits(Math.floor(num / 10)); return sum; ; console.log(sumOfDigits(35)); console.log(sumOfDigits(45));
-
数字的乘方
/** */ // calculate the power of a number const powOfNum = (num: number, pow: number) => if (!Number.isInteger(pow)) throw new Error('power can only be integer'); // base case, all number's pow of 0 is 1 if (pow === 0) return 1; if (pow < 0) return (1 / num) * powOfNum(num, pow + 1); return num * powOfNum(num, pow - 1); ; console.log(powOfNum(2, 2)); console.log(powOfNum(2, -2)); console.log(powOfNum(0.5, 2));
-
最大公约数
用的是欧几里得算法
/** */ // euclidean algorithm const gcd = (a: number, b: number) => if (!(Number.isInteger(a) && Number.isInteger(b))) throw new Error('both numbers must be integer'); if (b === 0) return a; if (a < 0) a = -a; if (b < 0) b = -b; return gcd(b, a % b); ; console.log(gcd(48, 18)); console.log(gcd(48, -18));
-
十进制转二进制
没有做 2’s complement
/** */ // euclidean algorithm const decimalToBinary = (num: number) => // did not implement 2's complement if (!Number.isInteger(num)) throw new Error('input should be an integer'); if (num == 0) return 0; return (num % 2) + decimalToBinary(Math.trunc(num / 2)) * 10; ; console.log(decimalToBinary(10)); console.log(decimalToBinary(-10));
-
阶乘
/** */ const factorial = (num: number) => if (!Number.isInteger(num)) throw new Error('input should be an integer'); if (num == 1) return 1; return num * factorial(num - 1); ; console.log(factorial(4));
-
数组的乘积和
/** */ const productOfArray = (arr: number[]) => if (!arr) throw new Error('input should be an array'); if (arr.length == 1) return arr[0]; return arr[0] * productOfArray(arr.slice(1)); ; console.log(productOfArray([1, 2, 3])); console.log(productOfArray([1, 2, 3, 4])); console.log(productOfArray([1, 2, 3, 10]));
-
获得区间和
0-n 的和
/** */ // accepts a number and adds up all the numbers from 0 to the number passed to the function const getRange = (num: number) => if (!Number.isInteger(num) || num < 0) throw new Error('input should be a positive integer'); if (num === 0) return num; return num + getRange(num - 1); ; console.log(getRange(6)); console.log(getRange(10));
-
斐波那契数字
/** */ const fibNum = (num: number) => if (!Number.isInteger(num) || num < 0) throw new Error('input should be a positive integer'); if (num < 2) return num; return fibNum(num - 1) + fibNum(num - 2); ; console.log(fibNum(4)); console.log(fibNum(10)); console.log(fibNum(28));
-
翻转字符串
/** */ const reverseStr = (str: string) => if (!str.length) return ''; return reverseStr(str.slice(1)) + str[0]; ; console.log(reverseStr('javascript')); console.log(reverseStr('typescript'));
-
是否为回文字符串
/** */ const isPalindrome = (str) => if (typeof str !== 'string') throw new Error('input should be a string'); if (!str.length || str.length === 1) return true; return ( str[0] === str[str.length - 1] && isPalindrome(str.substring(1, str.length - 1)) ); ; console.log(isPalindrome('abcba')); console.log(isPalindrome('tacocat')); console.log(isPalindrome('abbcad'));
-
some 递归调用回调函数
/** */ // array.some(cb) const someRecursiveCallback = ( arr: number[], cb: (num: number) => boolean ) => if (!arr) throw new Error('input should be an array'); if (arr.length === 1) return cb(arr[0]); if (!cb(arr[0])) return someRecursiveCallback(arr.slice(1), cb); return true; ; console.log(someRecursiveCallback([1, 2, 3, 4, 5], isOdd)); console.log(someRecursiveCallback([2, 4, 6], isOdd));
-
flat 数组
/** */ // arr.flat() const flatten = (arr: any) => arr.flat; if (!arr) throw new Error('input should be an array'); const res: any[] = []; for (const val of arr) if (val instanceof Array) res.push(...flatten(val)); else res.push(val); return res; ; console.log(flatten([1, [2, [3, [4, [5], 6]]]]));
-
大写数组中的首字母
/** */ const capitalizeFirst = (arr) => if (!arr) throw new Error('input should be an array'); if (!arr.length) return []; return [arr[0][0].toUpperCase() + arr[0].substring(1)].concat( ...capitalizeFirst(arr.slice(1)) ); ; console.log(capitalizeFirst(['car', 'wash', 'wasabi']));
-
计算对象中出现的和
/** */ const obj1 = outer: 2, obj: inner: 2, otherObj: superInner: 2, notANumber: true, alsoNotANumber: 'yup', , , ; const obj2 = a: 2, b: b: 2, bb: b: 3, bb: b: 2 , c: c: c: 2 , cc: 'ball', ccc: 5 , d: 1, e: e: e: 2 , ee: 'car' , ; const nestedEvenSum = (obj: any, sum = 0) => for (const [, val] of Object.entries(obj)) if (typeof val === 'number' && !isOdd(val)) sum += val; else if (val instanceof Object) sum += nestedEvenSum(val); return sum; ; console.log(nestedEvenSum(obj1));
以上是关于一些递归的练习题的主要内容,如果未能解决你的问题,请参考以下文章