当前位置:网站首页>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;
}
但是还是有一部分变态的输入情况,导致代码超时。 太无耻了这种输入情况
边栏推荐
- Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
- GCC [7] - compilation checks the declaration of functions, and link checks the definition bugs of functions
- AutoCAD - what is the default lineweight for centerline drawing and CAD? Can I modify it?
- 倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
- LeetCode-1279. Traffic light intersection
- 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
- How word displays modification traces
- RT-Thread 组件 FinSH 使用时遇到的问题
- swagger2报错Illegal DefaultValue null for parameter type integer
- Low CPU load and high loadavg processing method
猜你喜欢
ACTF 2022圆满落幕,0ops战队二连冠!!
全套教学资料,阿里快手拼多多等7家大厂Android面试真题
Dark horse -- redis
php+redis实现超时取消订单功能
zabbix 代理服务器 与 zabbix-snmp 监控
IC设计流程中需要使用到的文件
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
Analysis of frequent chain breaks in applications using Druid connection pools
安装Mysql报错:Could not create or access the registry key needed for the...
Low CPU load and high loadavg processing method
随机推荐
【翻译】供应链安全项目in-toto移至CNCF孵化器
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
Synchronous development of business and application: strategic suggestions for application modernization
五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
tensorflow和torch代码验证cuda是否安装成功
潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe
About image reading and processing, etc
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
IC设计流程中需要使用到的文件
Simple application of VBA script in Excel
反射及在运用过程中出现的IllegalAccessException异常
zabbix 代理服务器 与 zabbix-snmp 监控
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...
swagger2报错Illegal DefaultValue null for parameter type integer
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
Leetcode topic [array] - 119 Yang Hui triangle II