LoginSignup
0
0

More than 5 years have passed since last update.

山本山(回文)2

Last updated at Posted at 2014-06-24

 以前よりはコンパクトになった気がする!!(ArrayListに頼ってる所は変わらないけどね!!)前回分からなかったのは、自分として結構思う所があるので、克服出来る様にまずは他の問題できちんと正解できるようにしたいです!

import java.util.*;
public class Main {
    boolean isPalindrome(ArrayList<Character> s) {
        for(int r = 0; r < s.size()/2; r++) {
            if(s.get(r) != s.get(s.size() - 1 - r)) return(false);
        }
        return(true);
    }

    int find(String s) {
        ArrayList<Character> list = new ArrayList<Character>();
        for(int r = 0; r < s.length(); r++) {
            list.add(s.charAt(r));
        }
        if(isPalindrome(list)) return(list.size());
        boolean flag = false;
        int less = 0;
        while(!flag) {
            list.add(s.length(), s.charAt(less++));
            flag = isPalindrome(list);
        }
        return(list.size());
    }

    void doIt() {
        String s = "abab";
        String s2 = "abacaba";
        String s3 = "qwerty";
        String s4 = "abdfhdyrbdbsdfghjkllkjhgfds";

        System.out.println(find(s4));
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Main().doIt();
    }

}

ただこれ、回文作れない文字列の入力は想定されていないので、例外投げちゃうかと思います。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0