강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

robin님의 프로필 이미지
robin

작성한 질문수

10주완성 C++ 코딩테스트 | 알고리즘 코딩테스트

2-I

2 - I 투포인터를 이용한 자바 코드 입니다.

작성

·

325

0

투포인터를 이용해 subString() 의 범위를 결정하도록 하였습니다. 자바에서 큰 수를 다루는 클래스인 BigInteger 를 사용해 별도의 0 처리 없이 문제를 풀 수 있었습니다.

package lecture2;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Prob2870 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<BigInteger> result = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            result.addAll(findNums(sc.next()));
        }
        Collections.sort(result);
        result.forEach(System.out::println);
    }
    private static List<BigInteger> findNums(String str){
        int s =0,e=0;
        List<BigInteger> ret = new ArrayList<>();
        char[] chars = str.toCharArray();
        while (s<str.length() && e<str.length()){
            if(isNum(chars[s])){
                while (e<str.length() && isNum(chars[e])){
                    e++;
                }
                String result = str.substring(s,e);
                ret.add(new BigInteger(result));
                s = e;
            }else {
                s++;e++;
            }
        }
        return ret;
    }
    private static boolean isNum(char c){
        return c>='0' &&c<='9';
    }
}

답변 1

0

큰돌님의 프로필 이미지
큰돌
지식공유자

훌륭하다...

robin님의 프로필 이미지
robin

작성한 질문수

질문하기