Java for LeetCode 049 Anagrams

Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.

解题思路:首先要理解,什么是anagrams,ie。“tea”、“tae”、“aet”,然后就十分好做了,new一个hashmap,使用一个排过序的String作为key,重复了就往里面添加元素,这里出现一个小插曲,就是排序的时候不要用PriorityQueue,因为PriorityQueue是采用堆排序的,仅保证堆顶元素为优先级最高的(害了我好久)JAVA实现如下:

	static public List<String> anagrams(String[] strs) {
		List<String> list = new ArrayList<String>();
		HashMap<String, List<String>> hm = new HashMap<String, List<String>>();
		for (int i = 0; i < strs.length; i++) {
			char [] c=strs[i].toCharArray();
			Arrays.sort(c);
			String sortString=new String(c);
			if (!hm.containsKey(sortString))
				hm.put(sortString.toString(), new ArrayList<String>());
			hm.get(sortString.toString()).add(strs[i]);
		}
		Iterator<String> iterator = hm.keySet().iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			if (hm.get(key).size() > 1)
				list.addAll(hm.get(key));
		}
		return list;
	}
时间: 2024-11-08 22:32:28

Java for LeetCode 049 Anagrams的相关文章

LeetCode 049 Anagrams

题目要求:Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 分析: 参考网址:http://www.cnblogs.com/easonliu/p/3643595.html 代码如下: class Solution { public: vector<string> anagrams(vector<

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

Java for LeetCode 128 Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run i

Java for LeetCode 098 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

Java for LeetCode 057 Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge

Java for LeetCode 059 Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]] 解题思路: 参考Java for LeetCode 054 Spiral Matrix,修改下

Java for LeetCode 107 Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

Java for LeetCode 108 Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题思路: 首先要理解,什么叫做height balanced BST Java for LeetCode 110 Balanced Binary Tree,然后就十分容易了,JAVA实现如下: public TreeNode sortedArrayToBST(int[] nums) { return

Java for LeetCode 090 Subsets II

Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If nums = [1,2,2], a soluti