ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 문자열 뒤집기
    Computer Engineering/자료구조 2019. 9. 7. 22:03

    - Java

    public static void reverse(char[] s, int index) {
        if(index==0) {
            return;
        }else {
            System.out.print(s[index-1]);
        reverse(s, index-1);
        }
    }
    
    public static void main(String[] args) {
        char[] s = {'h','e','l','l','o'};
        reverse(s, s.length);
    }
    public static void reverse(char[] s, int f, int t) {
        if(f>t) {
            return;
        }else {
            reverse(s, f+1, t);
            System.out.print(s[f]);
        }
    }
    
    public static void main(String[] args) {
        char[] s = {'h','e','l','l','o'};
        reverse(s, 0, s.length-1);
    }

     

    - Swift

    func reverse(s: [Character], index: Int){
        if index == 0 {
            return
        }else {
            print(s[index-1], terminator: "")
            reverse(s: s, index: index-1)
        }
    }
    var s: [Character] = ["h","e","l","l","o"]
    reverse(s: s, index: s.count)

     

    [결과]

    반응형

    'Computer Engineering > 자료구조' 카테고리의 다른 글

    DFS(Depth First Search)  (0) 2019.09.23
    BFS(Breadth First Search)  (0) 2019.09.23
    재귀적 팩토리얼  (0) 2019.09.07

    댓글

Designed by Tistory.