[2019년 12월 30일가] 알고리즘 기록을 학습 - 디지털 출력은 세 번 나타납니다

알고리즘 - 디지털 출력은 세 번 나타납니다


예를 들어, 정수 문자열을 입력 : "1,2,2,2,3,4,4,4,6"모든 정수는 새로운 문자열 출력을 형성하기 위해 세 번 등장 선택합니다.
예 :
입력 : "1,2,2,2,3,4,4,4,6"
출력 : "24"

import java.util.HashMap;
import java.util.Map;


public class GetThree
{
    public int[] transfer(String list){
        String[] a = list.split(",");
        int size = a.length;
        int[] result = new int[size];
        for(int i = 0; i < a.length; i++){
            result[i] = Integer.parseInt( a[i] );
        }
        return result;
    }
    
    public String getResult(String orign){
        Map<Integer,Integer> map = new HashMap<Integer, Integer>();
        int[] orignList = transfer(orign);
        for(int i=0; i<orignList.length; i++){
            boolean containsKey = map.containsKey(orignList[i]);
            if(containsKey){   
             int value = map.get(orignList[i]) + 1;
             map.put(orignList[i], value);
            }
            else{
                map.put(orignList[i], 1);
            }            
        }
        String result = "";
        for(Integer key : map.keySet()){
            if(map.get(key) == 3){
                result += String.valueOf(key);
            }
        }
        return result;    
        }

    }


1. 문자열 및 다양한 포맷 간의 변환 :

  • 전송 문자열의 다른 유형 :
String s = String.valueOf( value); // 其中 value 为任意一种数字类型。
  • 문자열 전송 다른 유형 :
String s = "169"; 

byte b = Byte.parseByte( s ); 

short t = Short.parseShort( s ); 

int i = Integer.parseInt( s ); 

long l = Long.parseLong( s ); 

Float f = Float.parseFloat( s ); 

Double d = Double.parseDouble( s );

2. JAVA 분할 사용 :
EG

String[] splitAddress=address.split(",");
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);

"."구분 기호로 사용하는 경우는 다음과 같이 작성해야합니다 String.split("\\.")그래서 제대로 사용할 수 없습니다 분리에 관해서 String.split(".");
"와"* ", 이스케이프 문자있는, 추가해야 할 |" "때문에, 그리고". " 쉼표를 "\, 그리고.

기본 사용법 3. JAVA지도의 :
맵 키는 어떤 유형이 될 수 있습니다.
참조 보웬 1
참조 보웬 2
맵 containsKey () 맵에있어서의 핵심 요구를 포함 결정한다
맵을 분석하는 것은 가치 값으로써 containsValue ()에있어서의지도 사용해야 포함
참조 보웬 : 맵 분석 인지 키를 포함하도록 containsKey () 메소드를 매핑하는 데 필요한

참고 :

  1. 자바 문자열과 다양한 포맷 간의 변환
게시 17 개 원래 기사 · 원의 칭찬 0 · 조회수 332

추천

출처blog.csdn.net/cletitia/article/details/103786044