1. 程式人生 > 實用技巧 >MySQL中int(M)和tinyint(M)數值型別中M值的意義 - 【轉載】

MySQL中int(M)和tinyint(M)數值型別中M值的意義 - 【轉載】

找出字串中第一個只出現一次的字元

package my;

import java.util.HashMap;
//問題:在一個字串(0<=字串長度<=10000,全部由字母組成)中找到第一個只出現一次的字元,
//並返回它的位置, 如果沒有則返回 -1(需要區分大小寫).

public class FindFirstStringSolution {
    int  findFirstStr(String str){
        if(str.length() ==0 ||str == null){
            return -1;
        }
        HashMap
<Character, Integer> map = new HashMap<>(); for(int i=0 ; i < str.length() ; i++){ char c= str.charAt(i); if(map.containsKey(str.charAt(i))){ map.put(c,map.get(c)+1); }else{ map.put(c,1); } }
for(int j = 0 ; j<str.length(); j++){ if(map.get(str.charAt(j))== 1){ System.out.println(str.charAt(j)); return j; } } return -1; } public static void main(String[] args){ String str ="aabbcf33"; int n = new FindFirstStringSolution().findFirstStr(str); System.out.println(n); } }