当前位置:网站首页>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;
}
但是还是有一部分变态的输入情况,导致代码超时。 太无耻了这种输入情况
边栏推荐
- 如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
- 【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...
- Word如何显示修改痕迹
- 驼峰式与下划线命名规则(Camel case With hungarian notation)
- Dark horse -- redis
- Documents to be used in IC design process
- Characteristic colleges and universities, jointly build Netease Industrial College
- Camel case with Hungarian notation
- Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
- LeetCode_格雷编码_中等_89.格雷编码
猜你喜欢

面试突击63:MySQL 中如何去重?

An error occurs when installing MySQL: could not create or access the registry key needed for the

助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!

快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数

冒烟测试怎么做

Take a look at how cabloyjs workflow engine implements activiti boundary events

利用 clip-path 绘制不规则的图形

Synchronous development of business and application: strategic suggestions for application modernization
Benefit a lot, Android interview questions

学习探索-使用伪元素清除浮动元素造成的高度坍塌
随机推荐
Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
Zero foundation entry polardb-x: build a highly available system and link the big data screen
GCC [7] - compilation checks the declaration of functions, and link checks the definition bugs of functions
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
R语言使用order函数对dataframe数据进行排序、基于单个字段(变量)进行降序排序(DESCENDING)
Elastic search indexes are often deleted [closed] - elastic search indexes gets deleted frequently [closed]
A method of removing text blur based on pixel repair
Problems encountered in using RT thread component fish
打家劫舍III[后序遍历与回溯+动态规划]
通俗的讲解,带你入门协程
How to type multiple spaces when editing CSDN articles
第五期个人能力认证考核通过名单公布
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
swagger2报错Illegal DefaultValue null for parameter type integer
In 50W, what have I done right?
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the grouped dot strip plot, and set the add parameter to add box plots for different levels of dot strip
Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars
A popular explanation will help you get started