当前位置:网站首页>Leetcode 30. 串联所有单词的子串
Leetcode 30. 串联所有单词的子串
2022-07-06 11:34:00 【Java全栈研发大联盟】
代码如下:
public static List<Integer> findSubstring(String s, String[] words) {
List<Integer> result = new ArrayList<>();
int length = 0;
for (String word : words) {
length += word.length();
}
for (int i = 0; i <= s.length() - length; i++) {
String substring = s.substring(i, i + length);
boolean tran = tran(substring, words);
if (tran) {
result.add(i);
}
}
return result;
}
public static boolean tran(String str, String[] words) {
//处理边界情况
if (words.length == 1) {
return str.equals(words[0]);
}
boolean result = false;
for (int i = 0; i < words.length; i++) {
if (str.startsWith(words[i])) {
StringBuilder stringBuilder = new StringBuilder(str);
String substring = stringBuilder.delete(0, words[i].length()).toString();
String[] restWords = subWords(words, i);
if (tran(substring, restWords)) {
result = true;
}
}
}
return result;
}
public static String[] subWords(String[] words, int i) {
String[] strings = Arrays.copyOf(words, words.length);
for (int k = i; k < strings.length - 1; k++) {
strings[k] = strings[k + 1];
}
strings = Arrays.copyOfRange(strings, 0, strings.length - 1);
return strings;
}
但是还是有一部分变态的输入情况,导致代码超时。 太无耻了这种输入情况
边栏推荐
- 10 schemes to ensure interface data security
- Reflection and illegalaccessexception exception during application
- USB host driver - UVC swap
- Benefit a lot, Android interview questions
- Swagger2 reports an error illegal DefaultValue null for parameter type integer
- How can my Haskell program or library find its version number- How can my Haskell program or library find its version number?
- Black Horse - - Redis Chapter
- map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
- 【pytorch】yolov5 训练自己的数据集
- 受益匪浅,安卓面试问题
猜你喜欢
Pytorch common loss function
零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
Dark horse -- redis
RT-Thread 组件 FinSH 使用时遇到的问题
[paper notes] transunet: transformers make strongencoders for medical image segmentation
助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
Zero foundation entry polardb-x: build a highly available system and link the big data screen
How to access localhost:8000 by mobile phone
Black Horse - - Redis Chapter
随机推荐
It's super detailed in history. It's too late for you to read this information if you want to find a job
[translation] a GPU approach to particle physics
Translation D28 (with AC code POJ 26:the nearest number)
Don't miss this underestimated movie because of controversy!
Modulenotfounderror: no module named 'PIL' solution
主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
The slave i/o thread stops because master and slave have equal MySQL serv
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
short i =1; I=i+1 and short i=1; Difference of i+=1
潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
Digital "new" operation and maintenance of energy industry
[pytorch] yolov5 train your own data set
Looting iii[post sequence traversal and backtracking + dynamic planning]
R language uses the order function to sort the dataframe data, and descending sorting based on a single field (variable)
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
Benefit a lot, Android interview questions
思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
通俗的讲解,带你入门协程