코딩테스트10번 - 틀림

package main;

import java.util.Scanner;


public class Main {

	public int[] solu(String a, char b) {
	
	int d[] = new int[a.length()];
	
	int count = 0;
	

	
	for(int i = 0 ; i< a.length(); i ++) {
		if(a.charAt(i) == b) {
			count = 0;
			d[i] = count;
			
		}else {
			count++;
			d[i] = count;
		}
        	return d;
	}
    	
    	public static void main(String[] args) {
		
	
	
	Main main = new Main();
	
	Scanner scan = new Scanner(System.in);
	
	String a = scan.nextLine();
	
	char b = scan.next().charAt(0);
	
	main.solu(a,b);
	
	for (int x : main.solu(a, b)) {
	
		System.out.print(x+ " ");
	
}	}
	}
		

    

어려워서 인강을 봤습니다..

package main;

import java.util.Scanner;


public class Main {

	public int[] solu(String a, char b) {
	
	int d[] = new int[a.length()];
	
	int count = 1000;
	

	
	for(int i = 0 ; i< a.length(); i ++) {
		if(a.charAt(i) == b) {
			count = 0;
			d[i] = count;
			
		}else {
			count++;
			d[i] = count;
		}
	}
	
	
	
	count = 1000;
	for(int i = a.length()-1 ; i >= 0 ; i --) {
		if(a.charAt(i) == b) {
			count = 0;
		
			
		}else {
			count++;
			d[i] = Math.min(d[i], count);
		}
		
			}
	
	
	return d;
	}
		
	
	
	
	public static void main(String[] args) {
		
	
	
	Main main = new Main();
	
	Scanner scan = new Scanner(System.in);
	
	String a = scan.next();
	
	char b = scan.next().charAt(0);
	
	main.solu(a,b);
	
	for (int x : main.solu(a, b)) {
	
		System.out.print(x+ " ");
	
	}
	

	

	}
	
	
	

	
	}
		

좋은 웹페이지 즐겨찾기