Java 递归方法中for循环遇到返回语句 public void delete(字符串键){ 根=删除(根,键,0); } 公共节点删除(节点x,字符串键,int d){ 如果(x==null) 返回null; 如果(d==key.length()) x、 val=null; 否则{ 字符c=键字符(d); x、 next[c]=删除(x.next[c],键,d+1); } //---------------- 如果(x.val!=null) 返回x; 对于(char c=0;c< r;C++){ 如果(x.next[c]!=null) 返回x; 返回null; } }
摘自Algoritm 4一书,TrieST.java 发生了什么事Java 递归方法中for循环遇到返回语句 public void delete(字符串键){ 根=删除(根,键,0); } 公共节点删除(节点x,字符串键,int d){ 如果(x==null) 返回null; 如果(d==key.length()) x、 val=null; 否则{ 字符c=键字符(d); x、 next[c]=删除(x.next[c],键,d+1); } //---------------- 如果(x.val!=null) 返回x; 对于(char c=0;c< r;C++){ 如果(x.next[c]!=null) 返回x; 返回null; } },java,algorithm,dictionary,trie,Java,Algorithm,Dictionary,Trie,摘自Algoritm 4一书,TrieST.java 发生了什么事for循环可以多次返回x;返回类型方法可以吗?如果可以,那么最终返回值是多少?调用“return”后,函数终止 因此,函数返回的值为: 如果x.val!=空的 如果对于第一个c(即c=0),x.next[c]=空的 否则无效 正如评论中所说,我想一定是打字错误 public void delete(String key) { root = delete(root, key, 0); } public Node del
for
循环可以多次返回x
;返回类型方法可以吗?如果可以,那么最终返回值是多少?调用“return”后,函数终止
因此,函数返回的值为:
- 如果x.val!=空的
- 如果对于第一个c(即c=0),x.next[c]=空的
- 否则无效
public void delete(String key) {
root = delete(root, key, 0);
}
public Node delete(Node x, String key, int d) {
if(x == null)
return null;
if(d == key.length())
x.val = null;
else {
char c = key.charAt(d);
x.next[c] = delete(x.next[c], key, d + 1);
}
//----------------
if(x.val !=null )
return x;
for (char c = 0;c < R ; c++ ) {
if(x.next[c] != null)
return x;
return null;
}
}
应该在for循环之后。然后函数将返回:
- 如果x.val!=空的
- 如果存在一个c,那么x.next[c]=空的
- 否则无效
- 如果x.val!=空的
- 如果对于第一个c(即c=0),x.next[c]=空的
- 否则无效
public void delete(String key) {
root = delete(root, key, 0);
}
public Node delete(Node x, String key, int d) {
if(x == null)
return null;
if(d == key.length())
x.val = null;
else {
char c = key.charAt(d);
x.next[c] = delete(x.next[c], key, d + 1);
}
//----------------
if(x.val !=null )
return x;
for (char c = 0;c < R ; c++ ) {
if(x.next[c] != null)
return x;
return null;
}
}
应该在for循环之后。然后函数将返回:
- 如果x.val!=空的
- 如果存在一个c,那么x.next[c]=空的
- 否则无效