leetcode1055

article/2025/9/14 7:48:47

在这里插入图片描述
在这里插入图片描述
暴力查询即可,外面一层遍历target字符串,内层对source进行遍历,直到当前的内层遍历source中已无可匹配字符,在进行下一次source遍历,直到外层遍历结束。最终统计进行了几次内层遍历。
优化的方法:将内层遍历改为二分查找,进而衍生的需要用map存储source的每个字符对应的下标的列表。

public class test {public int shortestWay(String source, String target){Map<Character, List<Integer>> map = getCharIndexs(source);int res = 1;int index = -1;for(int i=0; i<target.length(); i++){char ch = target.charAt(i);int nextIndex = bsearch(map, ch, index);if(nextIndex == -1 && index == -1){return -1;}else if(nextIndex == -1){res++;index = -1;i--;}else{index = nextIndex;}}return res;}private Map<Character, List<Integer>> getCharIndexs(String src){Map<Character, List<Integer>> map = new HashMap<>();for(int i=0; i<src.length(); i++){char ch = src.charAt(i);map.computeIfAbsent(ch, k -> new ArrayList<>()).add(i);}return map;}private int bsearch(Map<Character, List<Integer>> map, char ch, int index){if(!map.containsKey(ch)){return -1;}List<Integer> array = map.get(ch);int l=0;int r=array.size()-1;while(l<r){int mid = l + (r-l)/2;if(array.get(mid) <= index){l = mid + 1;}else{r = mid;}}return array.get(l) > index ? array.get(l) : -1;}
}

http://chatgpt.dhexx.cn/article/8MoOOa9n.shtml

相关文章

Mysql报错1055

Mysql group by报错 1055 [Err] 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘xm_xhd.al.dy_uid’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_modeonly_full_…

MySQL 报错1055

一、问题描述 SELECT * FROM tbluser GROUP BY sex当我以这条语句进行数据库查询的时候&#xff0c;报了个错&#xff1a; > 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ceb.tbluser.uid which is not functiona…

mac mysql 1055_MySQL错误1055

问题描述:在MySQL数据库下,执行SQL插入语句报错。错误信息如下: 错误原因:在MySQL5.7之后,sql_mode中默认存在ONLY_FULL_GROUP_BY,SQL语句未通过ONLY_FULL_GROUP_BY语义检查所以报错。 ONLY_FULL_GROUP_BY:ONLY_FULL_GROUP_BY要求select语句中查询出来的列必须是明确的(…

MySQL 错误代码:1055 解决方案(推荐!!)

MySQL 错误代码:1055 解决办法 一、 MySQL版本二、 问题描述1. 问题描述2.ONLY_FULL_GROUP_BY-SQL示例 三、解决办法1.方法一2.方法二&#xff08;临时&#xff09;3.方法三&#xff08;永久&#xff09; 四、sql_mode常用值 一、 MySQL版本 MySQL版本&#xff1a;8.0.11 二、…

MySQL 出现1055错误 this is incompatible with sql mode=only full group by 的解决办法

文章目录 一、打开MySQL8.0 Command Line Client二、找到MySQL的my.ini文件路径三、修改my.ini里面的内容 前言 在学习过程中出现的问题&#xff1a;IDEA与Navicat出现不兼容的情况 会弹出1055的错误&#xff0c;这种错误通常在mysql 5.7以上的版本才会出现 1055 Expression #…

MySQL查询出现1055错误的解决方法

当SQL语句查询报1055错误时的解决方法 报错内容如下 报错原因 1.SQL语句中使用了group by&#xff0c;并且不需要分组的字段没有加上any_value()函数 2.MySQL数据库版本是大于5.7&#xff0c;报错信息中最后有一句sql_modeONLY_FULL_GROUP_BY&#xff0c;是因为MySQL数据库的…

1055 习题4-9-3 逆序输出正整数各位上数字

题目描述 输入一个不多于5位的正整数&#xff0c;按逆序输出各位上的数字&#xff0c;末尾换行。 注意&#xff1a;确保输入的正整数的位数不多于5。 输入 一个不多于5位的正整数。 输出 逆序输出各位上的数字&#xff0c;中间以空格分隔。 注意末尾的换行。 样例输入 2143 样…

IDEA破解后老是出现agent弹窗问题

第一步&#xff1a;在1这个路径下早到2这个文件 2.用解压软件提取2这个文件META-INF里面的important.txt放到上面1这个目录下&#xff08;这里使用的是7z解压&#xff09;

Intellij IDEA 破解方法和地址

IntelliJ IDEA是来自捷克的Jetbrains公司开发的Java开发工具(完爆Eclipse/MyEclipse)。有旗舰版和社区版两大版本&#xff0c;相对社区版而言旗舰版的功能更加丰富。官网提供了两个版本之间差异的详细比较。擅长企业级应用、移动应用以及Web应用开发。 支持MacOS,Windows7,Lin…

idea破解办法

在安装后启动的时候&#xff0c;Activate new license with选择License server&#xff0c;填上http://idea.iteblog.com/key.php&#xff0c;如图所示&#xff1a; 点击Activate&#xff0c;然后就搞定了。

Intellij idea破解以及运行优化

Intellij idea破解以及运行优化 idea 破解&#xff1a; 方式一&#xff1a;服务器破解&#xff0c;这种方式比较快速&#xff0c;但是很不稳定&#xff0c;所以不推荐 方式二&#xff1a;在校学生或者教师都可以免费申请&#xff0c;申请网址&#xff1a;https://www.jetbrain…

IDEA破解后无法启动

在网上找了破解IDEA的方法 原文&#xff1a;https://blog.csdn.net/qq_38637558/article/details/78914772 ①到这个地方下载 IntelliJ IDEA 注册码&#xff1a;http://idea.lanyus.com/ ②然后将这个jar包放入bin目录下&#xff0c;bin目录下&#xff0c;你的idea安装后的…

Intellij idea破解2017

最开始的时候intellij用得是社区版&#xff0c;因为博主没有钱去买专业版。。。但是后来发现社区版的功能确实弱了很多&#xff0c;比如流弊闪闪的Diagrams功能就没有…比如社区版不支持web项目&#xff0c;想起个tomcat跑个web项目都没法搞。于是&#xff0c;博主重新下载了个…

IDEA破解

① 到这个地方下载 IntelliJ IDEA 破解补丁&#xff1a;http://idea.lanyus.com/ 就是这个jar包&#xff1a;JetbrainsCrack-2.6.10-release-enc.jar ② 然后将这个jar包放入bin目录下&#xff0c;bin目录下&#xff0c;你的idea安装后的bin目录下 ③ 然后还是在这个bin目录…

IntelliJ IDEA破解

今天写个IDEA破解&#xff0c;废话不多说&#xff0c;直接给网址 官网&#xff1a;https://www.jetbrains.com/idea/ 破解注册码&#xff1a;http://idea.lanyus.com/ 里面有永久破解&#xff0c;还有注册码&#xff0c;可你根据你的喜爱&#xff0c;来选择&#xff0c;今天…

如何卸载 IDEA 破解补丁 Betterintellij ?

如何卸载 IDEA 破解补丁 Betterintellij ? 开始卸载 Betterintellij 补丁 这个补丁被黑客篡改过&#xff0c;有安全风险&#xff0c;强烈建议小伙伴们卸载掉&#xff0c;以免对自己造成损失。怎么卸载呢&#xff1f;跟着下文操作即可。 开始卸载 Betterintellij 补丁 1、在 …

idea破解之后查看使用日期

help的里面很多按钮&#xff0c;重置查看使用日期

idea破解失败无法打开

今天在学习使用spring boot 写接口的时候发现idea过期了然后查了一下如何破解&#xff0c;破解到了重启这一步之后就无法打开了&#xff0c;重装了很多次也没有用之后查了很久资料终于解决了&#xff0c;写个博客记录一下。 首先我是通过idea修改了idea64.exe.vmoptions文件 这…

IntelliJ IDEA下载安装教程

目录 友情提醒第一章、IDEA软件下载1.1&#xff09;官网下载 第二章、IDEA软件安装2.1&#xff09;以"ideaIU-2018.3.3.exe"为例3.2&#xff09;启动IDEA软件 友情提醒 点击文章目录可以跳转 第一章、IDEA软件下载 IDEA 全称 IntelliJ IDEA。在智能代码助手、代码…

idea破

百度下载一个 JetbrainsCrack-2.6.2.jar 破解补丁。放在你的安装idea下面的bin的目录下面。 下载链接&#xff1a;http://idea.lanyus.com/ 在安装的idea下面的bin目录下面有2个文件 &#xff1a; 一个是idea64.exe.vmoptions&#xff0c;还有一个是idea.exe.vmoptions 配置 …