当前位置:网站首页>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;
}
但是还是有一部分变态的输入情况,导致代码超时。 太无耻了这种输入情况
边栏推荐
- 接雨水问题解析
- 学习探索-使用伪元素清除浮动元素造成的高度坍塌
- R语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
- How to access localhost:8000 by mobile phone
- R language uses DT function to generate t-distribution density function data and plot function to visualize t-distribution density function data
- Tensorflow2.0 自定义训练的方式求解函数系数
- 数学知识——高斯消元(初等行变换解方程组)代码实现
- 深入分析,Android面试真题解析火爆全网
- test about BinaryTree
- 学习探索-函数防抖
猜你喜欢
![Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting](/img/dd/c3f4a9c38b156e3a9b9adfd6253773.gif)
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting

学习探索-无缝轮播图

Digital "new" operation and maintenance of energy industry

A popular explanation will help you get started

Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go

思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理

Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries

Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up

spark基础-scala
Benefit a lot, Android interview questions
随机推荐
Simple understanding of MySQL database
Php+redis realizes the function of canceling orders over time
MATLAB中deg2rad和rad2deg函数的使用
R language uses DT function to generate t-distribution density function data and plot function to visualize t-distribution density function data
安装Mysql报错:Could not create or access the registry key needed for the...
How can my Haskell program or library find its version number- How can my Haskell program or library find its version number?
R语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
The nearest library of Qinglong panel
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
In 50W, what have I done right?
It's super detailed in history. It's too late for you to read this information if you want to find a job
【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
A popular explanation will help you get started
打家劫舍III[后序遍历与回溯+动态规划]
数学知识——高斯消元(初等行变换解方程组)代码实现
业务与应用同步发展:应用现代化的策略建议
Reflection and illegalaccessexception exception during application
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
php+redis实现超时取消订单功能