龙空技术网

面试系列:使用递归实现字符串反转输出

IT技术百货 879

前言:

如今同学们对“字符串反转输出java 力扣”大致比较讲究,看官们都需要知道一些“字符串反转输出java 力扣”的相关资讯。那么小编同时在网摘上收集了一些关于“字符串反转输出java 力扣””的相关内容,希望我们能喜欢,兄弟们快快来学习一下吧!

字符串反转输出

例如:对于字符串abcdef,输出fedcba;要求使用递归实现

Python版本

# encoding: utf-8

def printReverse(str):

helper(0,str)

def helper(index,str):

if(index >= len(str)):returnelse:helper(index+1,str)print(str[index],end='')

if __name__ == "__main__":

print('abcdef') #abcdefprintReverse('abcdef')#fedcba

Java版本

class Untitled {

public static void printReverse(String str){

helper(0,str);

}

public static void helper(int index,String str){

if(index >= str.length()){

return;

}else{

helper(index+1,str);

}

System.out.print(str.charAt(index));

}

public static void main(String[] args) {

System.out.println("hello ");

printReverse("hello "); ///ul.loot//:sptth olleh

}

}

相关文章:

面试系列:手写HashMap的关键代码

面试系列:简答 为什么重写equasl必须重写HashCode

面试:经典排序算法思路及源码(收藏不看系列)

标签: #字符串反转输出java 力扣 #java将字符串反转输出