Java 返回字符串中第一个不重复字符的下标 下标从0开始
比如abcdefgabdef
其中字符c和g不重复,返回c的小标,下标从0开始,那么是2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | package com.example.demo; import org.testng.annotations.Test; import java.util.ArrayList; import java.util.LinkedHashSet; public class Test111 { String str1 = "abcdefgabdef" ; @Test public void fun1(){ //LinkedHashSet 有序的set集合 LinkedHashSet<Character> once = new LinkedHashSet<>(); LinkedHashSet<Character> chongFu = new LinkedHashSet<>(); ArrayList<Character> list = new ArrayList<>(); for ( int i = 0 ; i < str1.length(); i++) { boolean add = once.add(str1.charAt(i)); // 得到集合里所有出现过一次的元素 if (!add) { chongFu.add(str1.charAt(i)); // 得到集合里重复出现的元素 } } boolean removeAll = once.removeAll(chongFu); // once里将存储所有不重复的元素 list.addAll(once); System.out.println( "所有不重复的字母:" + list); for ( int i = 0 ; i < str1.length(); i++) { if (list.get( 0 ) == str1.charAt(i)) { System.out.println( "它的索引为:" + i); } } } @Test public void fun2(){ ArrayList<Character> list = new ArrayList<>(); for ( int i = 0 ; i < str1.length(); i++) { int flag = - 1 ; // 定义一个标志 for ( int j = 0 ; j < str1.length(); j++) { if (i == j) { // 如果是同一个元素跳过本次循环,继续下次循环 continue ; } // 如果这个元素不是第一次出现,将标志改变 if (str1.charAt(i) == str1.charAt(j)) { flag = 1 ; } } // 未改变标志的元素,即只出现一次的元素 if (flag == - 1 ) { list.add(str1.charAt(i)); } // if } // for System.out.println( "所有不重复的字母:" + list); System.out.println( "第一个不重复的字母为:" + list.get( 0 )); for ( int i = 0 ; i < str1.length(); i++) { if (list.get( 0 ) == str1.charAt(i)) { System.out.println( "它的索引为:" + i); } } } @Test public void fun3(){ for ( int i = 0 ; i < str1.length(); i++) { char charAt = str1.charAt(i); // 找出取出的元素第一次出现的位置 int indexOf = str1.indexOf(charAt); // 找出取出的元素最后一次出现的位置 int lastIndexOf = str1.lastIndexOf(charAt); // 如果是唯一的元素,第一次出现的位置和最后一次出现的位置应该相同 // 如果相同了就没必要在继续走for循环了 if (indexOf == lastIndexOf) { System.out.println(str1.charAt(indexOf)); System.out.println(indexOf); return ; } } } } |
BF算法
BF算法,即Brute Force 算法的简称。用于检测某个字符串是否是另一个字符串的子串。
子串的概念#
假设字符串 X = 'girlfriend' , Y = 'friend' ,那么Y 就是 X的子串。同样的,end是friend的子串。
BF算法的思路#
BF算法的思路非常简单粗暴,就是从前到后一点点的匹配。例如: 子串 x= abcd | 主串为 y = abdabceabcde
如果 x[0] = y[0] 继续比较 x[1] y[1] 继续下去,如果一直到 x[3]= y[3]时 则 x 整个串都匹配上了。
如果 x[0] = y[0] 继续比较 x[1] y[i] 发现不等,那么就开始用 x[0] 比较 y[1] 然后继续 x[1] y[2]。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | public class TestBF { public static void main(String[] args) { System.out.println(isSubstring( "abdabceabcde" , "abcd" )); // true System.out.println(isSubstring( "abdabceabcde" , "ff" )); // false } private static boolean isSubstring(String main,String sub){ if (main == null || sub == null ) { return false ; } if (main.length() < sub.length()) { // 子串不可比主串长 return false ; } if (main.equals(sub)) { // 主串,子串 相等的情况 return true ; } int len = main.length() - sub.length(); for ( int i = 0 ; i < len; i++) { boolean match = true ; for ( int j = 0 ; j < sub.length(); j++) { if (main.charAt(i+j) != sub.charAt(j)) { match = false ; break ; } } if (match) { return true ; } } return false ; } } |
![](https://images.cnblogs.com/cnblogs_com/qianjinyan/910455/o_jasmine%20love.gif)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 手把手教你更优雅的享受 DeepSeek
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现