当前位置:网站首页>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;
}
但是还是有一部分变态的输入情况,导致代码超时。 太无耻了这种输入情况
边栏推荐
- 包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
- Tensorflow and torch code verify whether CUDA is successfully installed
- Take a look at how cabloyjs workflow engine implements activiti boundary events
- C # use Marshall to manually create unmanaged memory in the heap and use
- Abstract classes and abstract methods
- Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
- Druid 数据库连接池 详解
- In depth analysis, Android interview real problem analysis is popular all over the network
- MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
- About image reading and processing, etc
猜你喜欢

AutoCAD - what is the default lineweight for centerline drawing and CAD? Can I modify it?

黑马--Redis篇

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

Detailed idea and code implementation of infix expression to suffix expression

【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。

A method of removing text blur based on pixel repair

IC设计流程中需要使用到的文件

通俗的讲解,带你入门协程

Druid 数据库连接池 详解

PMP每日一练 | 考试不迷路-7.6
随机推荐
php+redis实现超时取消订单功能
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
Synchronous development of business and application: strategic suggestions for application modernization
LeetCode-1279. Traffic light intersection
Problems encountered in using RT thread component fish
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
LeetCode_格雷编码_中等_89.格雷编码
受益匪浅,安卓面试问题
安装Mysql报错:Could not create or access the registry key needed for the...
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
Low CPU load and high loadavg processing method
GCC [7] - compilation checks the declaration of functions, and link checks the definition bugs of functions
C # - realize serialization with Marshall class
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
通俗的讲解,带你入门协程
Looting iii[post sequence traversal and backtracking + dynamic planning]
DaGAN论文解读
spark基础-scala