字符串反转输出
例如:对于字符串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 https://tool.lu/");
printReverse("hello https://tool.lu/"); ///ul.loot//:sptth olleh
}
}
相关文章:
面试系列:手写HashMap的关键代码
面试系列:简答 为什么重写equasl必须重写HashCode
面试:经典排序算法思路及源码(收藏不看系列)