当前位置:网站首页>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;
}
但是还是有一部分变态的输入情况,导致代码超时。 太无耻了这种输入情况
边栏推荐
- Leetcode topic [array] - 119 Yang Hui triangle II
- 面试突击63:MySQL 中如何去重?
- 凤凰架构2——访问远程服务
- 安装Mysql报错:Could not create or access the registry key needed for the...
- spark基础-scala
- zabbix 代理服务器 与 zabbix-snmp 监控
- Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
- Interview assault 63: how to remove duplication in MySQL?
- 保证接口数据安全的10种方案
- The second day of rhcsa study
猜你喜欢

Spark foundation -scala

安装Mysql报错:Could not create or access the registry key needed for the...

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

学习探索-无缝轮播图

Zero foundation entry polardb-x: build a highly available system and link the big data screen

zabbix 代理服务器 与 zabbix-snmp 监控

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

LeetCode-1279. Traffic light intersection

Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)

Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
随机推荐
USB host driver - UVC swap
ZABBIX proxy server and ZABBIX SNMP monitoring
R language ggplot2 visual time series histogram: visual time series histogram through two-color gradient color matching color theme
[pytorch] yolov5 train your own data set
Don't miss this underestimated movie because of controversy!
Synchronous development of business and application: strategic suggestions for application modernization
A method of removing text blur based on pixel repair
How to type multiple spaces when editing CSDN articles
First day of rhcsa study
Take a look at how cabloyjs workflow engine implements activiti boundary events
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
The list of people who passed the fifth phase of personal ability certification assessment was published
How to access localhost:8000 by mobile phone
R language uses the order function to sort the dataframe data, and descending sorting based on a single field (variable)
map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
CCNP Part 11 BGP (III) (essence)
通俗的讲解,带你入门协程
R language uses rchisq function to generate random numbers that conform to Chi square distribution, and uses plot function to visualize random numbers that conform to Chi square distribution
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
【计算情与思】扫地僧、打字员、信息恐慌与奥本海默