当前位置: 首页 > 面试经验 >

qq音乐数据岗笔试算法题

优质
小牛编辑
97浏览
2023-03-28

qq音乐数据岗笔试算法题


import java.util.ArrayList;
import java.util.List;

public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param s string字符串
* @param dic string字符串一维数组
* @return string字符串一维数组
*/
List<String> resArr = new ArrayList<String>();
String s;
String[] dic;

public String[] wordDiv(String s, String[] dic) {
// write code here
this.s = s;
this.dic = dic;

StringBuilder sb = new StringBuilder();
find(sb, 0);

String[] res = new String[resArr.size()];
for (int i = 0; i < resArr.size(); i++) {
res[i] = resArr.get(i);
}
return res;
}

public void find(StringBuilder sb1, int idx) {
if (idx != 0) sb1.append(" ");

for (int i = 0; i < dic.length; i++) {
StringBuilder sb = new StringBuilder(sb1);
if (dic[i].charAt(0) == s.charAt(idx)) {

if (s.length() - idx == dic[i].length()) {
for (int a = 0; a < dic[i].length(); a++) {
if (dic[i].charAt(a) != s.charAt(a + idx)) {
break;
}else if(a==dic[i].length()-1){
sb.append(dic[i]);
resArr.add(new String(sb));
}
}
} else {
for (int a = 0; a < dic[i].length(); a++) {
if (dic[i].charAt(a) != s.charAt(a + idx)) {
break;
}else if(a==dic[i].length()-1){
sb.append(dic[i]);
find(sb,idx+a+1);
}
}
}
}
}
}
}

 类似资料: