algorithm use java to implement