programmers level1 알고리즘 문제 - 문자열 내림차순으로 배치하기 Java
public class ReverseStr {
public String reverseStr(String str){
char str2[] = str.toCharArray();
int size = str2.length;
for ( int i =0;i<size;i++){
for(int j=1;j<size-i;j++){
if(str2[j-1]< str2[j])
{
char temp = str2[j-1];
str2[j-1] = str2[j];
str2[j] = temp;
}
}
}
return new String(str2);
}
// 아래는 테스트로 출력해 보기 위한 코드입니다.
public static void main(String[] args) {
ReverseStr rs = new ReverseStr();
System.out.println( rs.reverseStr("Zbcdefg") );
}
}
public String reverseStr(String str){
char str2[] = str.toCharArray();
int size = str2.length;
for ( int i =0;i<size;i++){
for(int j=1;j<size-i;j++){
if(str2[j-1]< str2[j])
{
char temp = str2[j-1];
str2[j-1] = str2[j];
str2[j] = temp;
}
}
}
return new String(str2);
}
// 아래는 테스트로 출력해 보기 위한 코드입니다.
public static void main(String[] args) {
ReverseStr rs = new ReverseStr();
System.out.println( rs.reverseStr("Zbcdefg") );
}
}
댓글
댓글 쓰기