zoukankan      html  css  js  c++  java
  • My Solution: Word Ladder

    public class Solution {
        public int ladderLength(String start, String end, Set<String> dict) {
    	if (start == null || end == null || dict == null
    				|| start.length() != end.length()) {
    			return 0;
    		}
    		if (oneDiff(start, end)) {
    
    			return 2;
    		}
    		return helper(start, end, dict, 2);
    
    	}
    
    	public boolean oneDiff(String left, String right) {
    		int sameNumber = 0;
    		if (left == null || right == null) {
    			return false;
    		}
    		if (left.length() != right.length()) {
    			return false;
    		}
    		for (int i = 0; i < left.length(); i++) {
    			if (left.charAt(i) == right.charAt(i)) {
    				sameNumber++;
    			}
    		}
    		if (sameNumber == left.length() - 1) {
    			return true;
    		}
    		return false;
    	}
    
    	public int helper(String start, String end, Set<String> dict, int level) {
    		Queue<String> queue = new LinkedList<String>();
    		queue.offer(start);
    		HashMap<String,Integer> route = new HashMap<String,Integer>();
    		route.put(start,0);
    		while (!queue.isEmpty()) {
    			int qSize = queue.size();
    			for (int i = 0; i < qSize; i++) {
    				String cur = queue.poll();
    				for (int j = 0; j < cur.length(); j++) {
    					for (char c = 'a'; c <= 'z'; c++) {
    						if (cur.charAt(j) == c) {
    							continue;
    						}
    						StringBuffer sb = new StringBuffer(cur);
    						sb.setCharAt(j, c);
    						String nowStr = sb.toString();
    
    						if (nowStr.equals(end)) {
    							return level++;
    						}
    						if (dict.contains(nowStr)
    								&& !route.containsKey(nowStr)) {
    							queue.offer(nowStr);
    							route.put(nowStr,0);
    
    						}
    					}
    				}
    
    			}
    			level++;
    		}
    		return 0;
    	}
    }



    之后发现这个太丑了,用了算法导论上的方法,找一个东西来记录层数:
    public class Solution {
    	public int ladderLength(String start, String end, Set<String> dict) {
    		if (start == null || end == null || dict == null
    				|| start.length() != end.length()) {
    			return 0;
    		}
    	
    		return helper(start, end, dict, 1);
    
    	}
    
    	public int helper(String start, String end, Set<String> dict, int level) {
    		Queue<String> queue = new LinkedList<String>();
    		queue.offer(start);
    		HashMap<String, Integer> route = new HashMap<String, Integer>();
    		route.put(start, 1);
    		while (!queue.isEmpty()) {
    			String cur = queue.poll();
    			int curLevel = route.get(cur);
    
    			for (int j = 0; j < cur.length(); j++) {
    				for (char c = 'a'; c <= 'z'; c++) {
    					if (cur.charAt(j) == c) {
    						continue;
    					}
    					StringBuffer sb = new StringBuffer(cur);
    					sb.setCharAt(j, c);
    					String nowStr = sb.toString();
    
    					if (nowStr.equals(end)) {
    						return ++curLevel;
    					}
    					if (dict.contains(nowStr) && !route.containsKey(nowStr)) {
    						queue.offer(nowStr);
    						route.put(nowStr, curLevel + 1);
    
    					}
    				}
    			}
    
    		}
    		return 0;
    
    	}
    }


  • 相关阅读:
    如何删除windows服务zz 重新安装PostgreSQL时删除上次遗留service的方法
    如何配置OGRE 1.7.0+CEGUI 0.7.1
    [原]一个由memset引发的知识点
    ArcGis测距问题
    自己动手,制作.net35离线安装包
    TTS语音合成
    Acess字段名用到与系统冲突的特殊名时的处理
    程序运行长期等待时显示等待动画
    修改Windows 2003 SOCKET端口数量默认5000限制
    服务器上发布的网站应用80端口时内网可以访问,外网不能访问
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4013063.html
Copyright © 2011-2022 走看看