当前位置:网站首页>Leetcode 30. Concatenate substrings of all words
Leetcode 30. Concatenate substrings of all words
2022-07-06 19:37:00 【Java full stack R & D Alliance】
The code is as follows :
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) {
// Dealing with border situations
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;
}
But there is still some abnormal input , Cause code timeout . It's shameless. This kind of input
边栏推荐
- Spark foundation -scala
- MySQL information schema learning (I) -- general table
- A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
- spark基础-scala
- Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
- [translation] a GPU approach to particle physics
- 如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
- 今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
- 快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
- 深度剖析原理,看完这一篇就够了
猜你喜欢
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
Interpretation of Dagan paper
Application of clock wheel in RPC
CPU负载很低,loadavg很高处理方法
MySQL information schema learning (I) -- general table
通俗的讲解,带你入门协程
IC设计流程中需要使用到的文件
保证接口数据安全的10种方案
Teach you to learn JS prototype and prototype chain hand in hand, a tutorial that monkeys can understand
Computer network: sorting out common network interview questions (I)
随机推荐
Mysql Information Schema 学习(一)--通用表
手把手教你学会js的原型与原型链,猴子都能看懂的教程
Learning and Exploration - Seamless rotation map
short i =1; i=i+1与short i=1; i+=1的区别
Unbalance balance (dynamic programming, DP)
PMP practice once a day | don't get lost in the exam -7.6
The second day of rhcsa study
In depth analysis, Android interview real problem analysis is popular all over the network
力扣101题:对称二叉树
MySQL information schema learning (I) -- general table
CPU负载很低,loadavg很高处理方法
Mysql Information Schema 学习(二)--Innodb表
A popular explanation will help you get started
MATLAB中deg2rad和rad2deg函数的使用
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
English topic assignment (25)
Using clip path to draw irregular graphics
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
Application of clock wheel in RPC