【发布时间】:2019-12-03 00:45:44
【问题描述】:
我已经研究这种方法有一段时间了,我很确定这是一个简单的解决方案。在我的一生中,我无法弄清楚为什么每当我传递[false, true, true, false, false, false, false, false, true, true, false, true, false, true, false, false, true, true, false, false, true, true, true, false, true, true] 的布尔数组时我的代码返回strin。出于某种原因,“g”从未出现在字符串中,我似乎无法弄清楚为什么。
public String decodeIter(boolean[] coding)
{
String str = "";
Node current = root;
int i = 0;
while(i < coding.length)
{
if(current != null)
{
if(current.left == null && current.right == null)
{
LeafNode leaf = (LeafNode) current;
str += leaf.data;
current = root;
} else {
if(coding[i] == true)
{
current = current.right;
i++;
} else {
current = current.left;
i++;
}
}
}
}
return str;
}
任何帮助将不胜感激。
【问题讨论】:
-
str += leaf.data;是您添加到 str 的唯一时间。也许leaf.data总是对或错? -
@KnowNoTrend leaf.data 是一个字符
标签: java tree huffman-code