zoukankan      html  css  js  c++  java
  • Java 返回字符串中第一个不重复字符的下标 下标从0开始

    比如abcdefgabdef

    其中字符c和g不重复,返回c的小标,下标从0开始,那么是2

     

    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]。

    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;
        }
    
    }
    

      

      

  • 相关阅读:
    关于游戏
    学习lucene5.5.4的笔记
    lucene中文学习地址推荐
    lucene的使用与优化
    进一步了解this和super
    被遗忘的设计模式——空对象模式(Null Object Pattern)
    Java 空对象设计模式(Null Object Pattern) 讲解
    java的动态代理机制详解
    为什么要使用代理模式
    大O 表示法
  • 原文地址:https://www.cnblogs.com/qianjinyan/p/10823525.html
Copyright © 2011-2022 走看看