当前位置:网站首页>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;
}
但是还是有一部分变态的输入情况,导致代码超时。 太无耻了这种输入情况
边栏推荐
- Use of deg2rad and rad2deg functions in MATLAB
- 终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
- Analysis of frequent chain breaks in applications using Druid connection pools
- usb host 驱动 - UVC 掉包
- Test technology stack arrangement -- self cultivation of test development engineers
- Don't miss this underestimated movie because of controversy!
- Is not a drawable (color or path): the vector graph downloaded externally cannot be called when it is put into mipmap, and the calling error program crashes
- MRO工业品企业采购系统:如何精细化采购协同管理?想要升级的工业品企业必看!
- 关于图像的读取及处理等
- IC设计流程中需要使用到的文件
猜你喜欢

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

LeetCode_双指针_中等_61. 旋转链表

ROS自定义消息发布订阅示例

数学知识——高斯消元(初等行变换解方程组)代码实现

Php+redis realizes the function of canceling orders over time

Druid database connection pool details

黑馬--Redis篇

Abstract classes and abstract methods

PMP practice once a day | don't get lost in the exam -7.6

五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
随机推荐
How to access localhost:8000 by mobile phone
LeetCode-1279. 红绿灯路口
GCC [7] - compilation checks the declaration of functions, and link checks the definition bugs of functions
打家劫舍III[后序遍历与回溯+动态规划]
PMP每日一练 | 考试不迷路-7.6
C # - realize serialization with Marshall class
usb host 驱动 - UVC 掉包
The slave i/o thread stops because master and slave have equal MySQL serv
tensorflow和torch代码验证cuda是否安装成功
JDBC详解
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
冒烟测试怎么做
spark基础-scala
Interface test tool - postman
学习探索-使用伪元素清除浮动元素造成的高度坍塌
潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe
Word如何显示修改痕迹
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
Analysis of frequent chain breaks in applications using Druid connection pools
Based on butterfly species recognition