【发布时间】:2018-05-29 14:51:55
【问题描述】:
给定方法:
public String moveSmallest(String s) {}
如何找到具有最小 ASCII 值的字符,将其放在字符串的末尾,并返回该字符串,递归?
阅读Find a char..、Move the char..、Move to end.. 并没有真正回答:我需要什么算法,如何实现。
不能使用全局变量、累加器、辅助方法或任何其他结构。
这个问题与How to pass a partial solution to the next recursive call密切相关(本质上)。
尝试查找最小字符:
if (s.length() == 0) {
return s;
} else if (s.length() > 1) {
char c = s.charAt(0) > moveSmallest(s.substring(1)).charAt(s.length()-1) ? s.charAt(0) : moveSmallest(s.substring(1)).charAt(s.length()-1);
}
【问题讨论】:
-
你尝试过什么吗?