当前位置:网站首页>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
边栏推荐
- zabbix 代理服务器 与 zabbix-snmp 监控
- 从sparse.csc.csr_matrix生成邻接矩阵
- 数学知识——高斯消元(初等行变换解方程组)代码实现
- Is not a drawable (color or path): the vector graph downloaded externally cannot be called when it is put into mipmap, and the calling error program crashes
- The slave i/o thread stops because master and slave have equal MySQL serv
- How to type multiple spaces when editing CSDN articles
- Cereals Mall - Distributed Advanced p129~p339 (end)
- 【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
- LeetCode_ Gray code_ Medium_ 89. Gray code
- How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
猜你喜欢
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
系统性详解Redis操作Hash类型数据(带源码分析及测试结果)
A method of removing text blur based on pixel repair
Documents to be used in IC design process
spark基础-scala
【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
LeetCode_ Double pointer_ Medium_ 61. rotating linked list
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
JDBC详解
随机推荐
A method of removing text blur based on pixel repair
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
Tensorflow and torch code verify whether CUDA is successfully installed
Translation D28 (with AC code POJ 26:the nearest number)
Lick the dog until the last one has nothing (simple DP)
Dom 操作
In depth analysis, Android interview real problem analysis is popular all over the network
LeetCode_格雷编码_中等_89.格雷编码
ZABBIX proxy server and ZABBIX SNMP monitoring
Computer network: sorting out common network interview questions (I)
Hudi vs Delta vs Iceberg
Simple application of VBA script in Excel
算法面试经典100题,Android程序员最新职业规划
IC设计流程中需要使用到的文件
JDBC details
1805. 字符串中不同整数的数目
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
C # use Marshall to manually create unmanaged memory in the heap and use
Phoenix Architecture 2 - accessing remote services