当前位置:网站首页>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;
}
但是还是有一部分变态的输入情况,导致代码超时。 太无耻了这种输入情况
边栏推荐
- 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
- JDBC详解
- C # use Marshall to manually create unmanaged memory in the heap and use
- 凤凰架构2——访问远程服务
- R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置add参数为不同水平点状条带图添加箱图
- 数学知识——高斯消元(初等行变换解方程组)代码实现
- Simple application of VBA script in Excel
- Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
- Php+redis realizes the function of canceling orders over time
- R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize dot plot, set the palette parameter, and set the colors of data points and box graphs of dot plots at differ
猜你喜欢

Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers

The list of people who passed the fifth phase of personal ability certification assessment was published

Php+redis realizes the function of canceling orders over time

Mysql Information Schema 学习(二)--Innodb表

Detailed idea and code implementation of infix expression to suffix expression

冒烟测试怎么做

Characteristic colleges and universities, jointly build Netease Industrial College

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

PMP每日一练 | 考试不迷路-7.6

Pytorch common loss function
随机推荐
受益匪浅,安卓面试问题
PMP practice once a day | don't get lost in the exam -7.6
zabbix 代理服务器 与 zabbix-snmp 监控
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
Characteristic colleges and universities, jointly build Netease Industrial College
黑馬--Redis篇
R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize dot plot, set the palette parameter, and set the colors of data points and box graphs of dot plots at differ
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
Looting iii[post sequence traversal and backtracking + dynamic planning]
接雨水问题解析
LeetCode-1279. 红绿灯路口
R语言ggplot2可视化:使用ggpubr包的ggdotplot函数可视化点阵图(dot plot)、设置palette参数设置不同水平点阵图数据点和箱图的颜色
ROS自定义消息发布订阅示例
【pytorch】yolov5 训练自己的数据集
深入分析,Android面试真题解析火爆全网
RT-Thread 组件 FinSH 使用时遇到的问题
In depth analysis, Android interview real problem analysis is popular all over the network
黑马--Redis篇
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告