其他分享
首页 > 其他分享> > (Medium) Alphabet Board Path - LeetCode

(Medium) Alphabet Board Path - LeetCode

作者:互联网

 

Partially Correct, 

When there appears to be Z, the path returned is not correct.

still need to fix.

 

class Solution {
    public String alphabetBoardPath(String target) {
        
        String[] board= {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"}; 
        
        if(target ==null || target.length() ==0){
            return null;
        }
        
        String out="";
        
        int row = 0;
        int col = 0;
        
        
        
        for(int i =0; i<target.length();i++){
            
            char key = target.charAt(i);
            
            if(i>0 && target.charAt(i)==target.charAt(i-1)){
                out = out+"!";
                continue;
            }
            
            for(int j =0; j< board.length;j++){
                
                if(board[j].indexOf(key)!= -1){
                    
                    row = j-row;
                    col = board[j].indexOf(key)-col;
                    
                    if(row >0){
                        
                        out = out + repeat("D",row);
                    }
                    
                    else{
                        out =out +repeat("U",(-row));
                        
                    }
                    
                    if(col>0){
                        
                        out = out + repeat("R",col);
                    }
                    
                    else {
                        
                        out = out +repeat("L", -col);
                        
                    }
                    
                    out=out+"!";
                    
                    row =j;
                    col = board[j].indexOf(key);
                    break;
                }
            }
            
           
        }
        
        return out;
        
    }
    
    public  String repeat(String a, int b){
    
    String out ="";
    
    for(int i =0;i<b;i++)
    {
      out = out+a;
    }
     return out;
  }
    
    
}

 

标签:Medium,String,int,Alphabet,board,Board,out,col,row
来源: https://www.cnblogs.com/codingyangmao/p/11289069.html