我对JAVA很新。我想为两个版本的回文测试方法中的每个版本绘制两个输入字符串“repaper”和“refinisher”的执行跟踪树
public static void main( String[] args ) {
String testCase1 = "repaper";
String testCase2 = "refinisher";
if ( isPalindrome_Version_1 ( testCase1 ) )
System.out.println ( "The string \"" + testCase1 + "\" is a palindrome" );
else
System.out.println ( "The string \"" + testCase1 + "\" is NOT a palindrome" );
}
// return "true" iff the String s is a palindrome
private static boolean isPalindrome_Version_1 ( String s )
{
boolean result = false;
// BASE CASE
if ( s.length() <= 1 )
return (true);
// BASE CASE - check if first char equals last char
if ( s.charAt(0) != s.charAt ( s.length() - 1 ) )
return ( false );
// GENERAL CASE - check whether the substring formed by removing
// the first and last letters is a palindrome
result = isPalindrome_Version_1 ( s.substring ( 1, s.length() - 1 ) );
return( result );
}
// return "true" iff the String s is a palindrome
private static boolean isPalindrome_Version_2 ( String s )
{
boolean result = false;
// BASE CASE
if ( s.length() <= 1 )
return (true);
// GENERAL CASE - check whether the substring formed by removing
// the first and last letters is a palindrome
result = isPalindrome_Version_2 ( s.substring ( 1, s.length() - 1 ) );
// BASE CASE - check if first char equals last char
if ( s.charAt(0) != s.charAt ( s.length() - 1 ) )
return ( false );
return( result );
}
}
答案 0 :(得分:0)
function Player(username, lvl, exp, gold, hp, atk, def, spd) {
var self = this;
this.username = username;
this.lvl = lvl;
this.exp = exp;
this.gold = gold;
this.hp = hp;
this.atk = atk;
this.def = def;
this.spd = spd;
this.implement = function() {
var h1 = document.getElementById('user');
h1.innerText = this.username;
h1.addClass('playerName');
$(h1).data('player', self)
}
this.implement();
}
var newPlayer = new Player(prompt("What is your username?"), 1, 0, 0, 10, 2, 2, 2);
playerEl = $('.playerName');
player = playerEl.data('player');
或类似的东西,知道递归的深度
private static boolean isPalindrome_Version_2 ( String s)
{
boolean result = false;
// BASE CASE
if ( s.length() <= 1 ){
System.out.println("Input Value :" + s + "Return Value" + true);
return (true);
// GENERAL CASE - check whether the substring formed by removing
// the first and last letters is a palindrome
result = isPalindrome_Version_2 ( s.substring ( 1, s.length() - 1 ) );
// BASE CASE - check if first char equals last char
if ( s.charAt(0) != s.charAt ( s.length() - 1 ) )
{
System.out.println("Input Value :" + s + "Return Value" + false);
return ( false );
}
System.out.println("Input Value :" + s + "Return Value" + result);
return( result );
}
顺便说一下,你有另一个递归的解决方案
private static boolean isPalindrome_Version_2 ( String s, int level)
{
boolean result = false;
// BASE CASE
if ( s.length() <= 1 ){
System.out.println("Input Value :" + s + "Return Value" + true + "Recursion Level" + level);
return (true);
// GENERAL CASE - check whether the substring formed by removing
// the first and last letters is a palindrome
result = isPalindrome_Version_2 ( s.substring ( 1, s.length() - 1 ), level ++ );
// BASE CASE - check if first char equals last char
if ( s.charAt(0) != s.charAt ( s.length() - 1 ) )
{
System.out.println("Input Value :" + s + "Return Value" + false+ "Recursion Level" + level);
return ( false );
}
System.out.println("Input Value :" + s + "Return Value" + result+ "Recursion Level" + level);
return( result );
}
答案 1 :(得分:0)
我不认为第2版是正确的(或者至少是有效的)因为你在递归整个字符串后返回false ...
话虽如此,这是第1版的痕迹
isPalindrome("repaper") // length greater than 1 and ending characters are equal
isPalindrome("epape") // length greater than 1 and ending characters are equal
isPalindrome("pap") // length greater than 1 and ending characters are equal
isPalindrome("a") // length is 1, return true
测试案例2
isPalindrome("refinisher") // length greater than 1 and ending characters are equal
isPalindrome("efinishe") // length greater than 1 and ending characters are equal
isPalindrome("finish") // length greater than 1 but ending characters are not equal, return false
你现在应该能够自己做第2版