Reverse string using recursion
On-Site Question 3 - SOLUTION
Question
Given a string, write a function that uses recursion to reverse it.
Requirements
You MUST use pen and paper or a whiteboard to answer this, no coding allowed!
SOLUTION
Hopefully you remember this problem, you've already seen it! The solution is:
def reverse(s): # Base Case
if len(s) <= 1:
return s # Recursion
return reverse(s[1:]) + s[0]
Notes
Remember when recursion questions arise, think about the base case and the recursive case. Review the recusion section of the course for review for this problem.
Good Job!
Reverse string using recursion的更多相关文章
- 344. Reverse String【easy】
344. Reverse String[easy] Write a function that takes a string as input and returns the string rever ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- LeetCode Reverse String
原题链接在这里:https://leetcode.com/problems/reverse-string/ 题目: Write a function that takes a string as in ...
- Nim Game,Reverse String,Sum of Two Integers
下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...
- [CareerCup] 1.2 Reverse String 翻转字符串
1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string ...
- 344. Reverse String(C++)
344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- [LeetCode] Reverse String II 翻转字符串之二
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
- LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers
344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...
随机推荐
- 获取ASPxGridView 中的数据(仅仅是获取;注意模板是如何获取的)
1.取得控件值 using System.Collections.Generic; //取得当前控件值的集合 直接寻找控件的ID List<object> keyValues = this ...
- ubuntu编译安装php7, 安装openssl
sudo apt-get install openssl sudo apt-get install libssl-dev
- Haskell语言学习笔记(58)Bifoldable
Bifoldable class Bifoldable p where bifold :: Monoid m => p m m -> m bifold = bifoldMap id id ...
- Haskell语言学习笔记(45)Profunctor
Profunctor class Profunctor p where dimap :: (a -> b) -> (c -> d) -> p b c -> p a d d ...
- iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 错误处理
沪江CCtalk视频地址:https://www.cctalk.com/v/15114923887518 处理错误请求 爱能遮掩一切过错. 当我们在访问一个站点的时候,如果访问的地址不存在(404), ...
- JAVA数组详解
package com.keke.demo; import java.text.ParseException;import java.text.SimpleDateFormat;import java ...
- C#整合VS2010和NUnit
软件下载 .Net单元测试工具 NUnit下载:http://www.nunit.org/index.php?p=download,最新的为NUnit-2.6.0.12051.msi,下载安装. VS ...
- 构造函数中的super和this的使用
super用于调用父类构造函数的部分,其必须出现在构造函数的第一行.super在调用时第一件事就是去执行父类构造函数的部分,所执行的父类构造函数与super()括号中的参数相对应. this用于在一个 ...
- 利用R产生随机数
生成随机数有两个函数runif()和rnorm(),其中r表示的是random随机的意思,unif表示的是均匀分布,而norm表示的是正态分布. 1)生成10个2到3之间的,服从均匀分布的随机数:ru ...
- 杨辉三角(生成器generator)
生成器:(Python中,这种一边循环一边计算的机制,称为生成器:generator) 创建generator的方法: 1.把列表生成式的[]变为(),就创建了一个generator 例: 可以通过n ...