去哪儿笔试编程题目AC代码分享

//进制转换
public class S1 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			String str = sc.next();
			if (str == null || str.length() == 0)
				return;
			long res = 0;
			for (int j = 0; j <= str.length() - 1; j++)
				res = res * 26 + (str.charAt(j) - 'a');
			System.out.println(res);
		}
	}
}

//层次遍历
class TreeNode {
	int val;
	TreeNode left;
	TreeNode right;

	public TreeNode(int v) {
		val = v;
	}

}

public class s2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int pre[] = new int[n];
		int in[] = new int[n];
		for (int i = 0; i < n; i++)
			pre[i] = sc.nextInt();
		for (int i = 0; i < n; i++)
			in[i] = sc.nextInt();
		TreeNode root = resCons(pre, 0, n - 1, in, 0, n - 1);
		order(root);
	}

	private static void order(TreeNode root) {
		if (root == null)
			return;
		Queue<TreeNode> q = new LinkedList<TreeNode>();
		q.offer(root);
		while (!q.isEmpty()) {
			TreeNode t = q.poll();
			System.out.print(t.val + " ");
			if (t.left != null)
				q.offer(t.left);
			if (t.right != null)
				q.offer(t.right);

		}
	}

	private static TreeNode resCons(int[] pre, int startPre, int endPre, int[] in, int startIn, int endIn) {
		if (startPre > endPre || startIn > endIn)
			return null;
		TreeNode root = new TreeNode(pre[startPre]);
		for (int i = startIn; i <= endIn; i++)
			if (in[i] == pre[startPre]) {
				root.left = resCons(pre, startPre + 1, startPre + i - startIn, in, startIn, i - 1);
				root.right = resCons(pre, i - startIn + startPre + 1, endPre, in, i + 1, endIn);
			}
		return root;
	}
}

//word ladder
public class s3 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String start = sc.nextLine();
		String end = sc.nextLine();
		String temp = sc.nextLine();
		String[] dicts = temp.split("\\s+");
		List<String> dict = new ArrayList<String>();
		for (int i = 0; i < dicts.length; i++)
			dict.add(dicts[i]);
		System.out.println(ladderLength(start, end, dict));
	}

	public static int ladderLength(String beginWord, String endWord, List<String> wordList) {
		if (beginWord == null || endWord == null || wordList.size() == 0)
			return 0;
                Set<String> set=new HashSet<String>(wordList);
		if(set.contains(beginWord))
		        set.remove(beginWord); 
               if (beginWord.equals(endWord))
			return 2;
		if (beginWord.length() != endWord.length())
			return 0;
		Queue<String> q = new LinkedList<String>();
		// map储存该单词的层数
		Map<String, Integer> map = new HashMap<String, Integer>();
		q.offer(beginWord);
		map.put(beginWord, 1);
		while (!q.isEmpty()) {
			String temp = q.poll();
			int level = map.get(temp);
			for (int j = 0; j < temp.length(); j++) {
				char[] words = temp.toCharArray();
				for (char i = 'a'; i <= 'z'; i++) {
					words[j] = i;
					String word = new String(words);
					if (set.contains(word)) {
						if (word.equals(endWord))
							return level + 1;
						map.put(word, level + 1);
						q.offer(word);
						set.remove(word);
					}

				}
			}
		}
		return 0;
	}
}


全部评论
你三道都AC了啊?
点赞 回复 分享
发布于 2017-04-02 17:04

相关推荐

评论
点赞
8
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务