zoukankan      html  css  js  c++  java
  • 1138. 字母板上的路径


    曼哈顿距离

    class Solution {
    
    	public String alphabetBoardPath(String target) {
    		int x = 0, y = 0, nx = 0, ny = 0;
    		StringBuffer ans = new StringBuffer("");
    		for (int i = 0; i < target.length(); i++) {
    			int temp = target.charAt(i) - 'a' ;
    			if (i > 0 && target.charAt(i) == target.charAt(i - 1)) {
    				ans.append("!");
    			} else {
    				nx = temp / 5;
    				ny = temp % 5;
    				if (ny < y) {
    					for (int z = 0; z < y - ny; z++)
    						ans.append("L");
    				}
    				if (nx < x) {
    					for (int z = 0; z < x - nx; z++)
    						ans.append("U");
    				}
    				if (nx > x) {
    					for (int z = 0; z < nx - x; z++)
    						ans.append("D");
    				}
    				if (ny > y) {
    					for (int z = 0; z < ny - y; z++)
    						ans.append("R");
    				}
    				
    				ans.append("!");
    				x = nx;
    				y = ny;
    				
    			}
    		}
    		return ans.toString();
    	}
    }
    
  • 相关阅读:
    vue
    vue
    vue 中使用style(样式)
    vue 中使用class(样式)
    第17课
    第16课
    第15课
    第14课
    第13课
    第12课
  • 原文地址:https://www.cnblogs.com/cznczai/p/11289376.html
Copyright © 2011-2022 走看看