I'm into the shadow of the road (1)

This series of algorithms to record I do question

Ouye
1, spelling words
spelling the word original title link

Give you a "Glossary" (array of strings) words and an "alphabet" (string) chars.

If you can spell out words in a "word" (string) with the chars in the "letter" (character), then we think you've mastered the word.

Note: Each time the spelling, chars each letter can only be used once.

Return vocabulary words in all the words you have mastered the sum of the lengths.

Example 1:

Input: words = [ "cat", "bt", "hat", "tree"], chars = "atach"
Output: 6
Explanation:
may form a string "cat" and "hat", so the answer is 3 + 3 = 6.

Example 2:

Input: words = [ "hello", "world", "leetcode"], chars = "welldonehoneyr"
Output: 10
Explanation:
may form a string "hello" and "world", so the answer is 5 + 5 = 10.

prompt:

. 1 <= words.length <= 1000
. 1 <= words [I] .length, chars.length <= 100
all strings are only lowercase letters

Source: stay button (LeetCode)
This question means that in a given array of strings to find strings exist in a given character array

  public int countCharacters(String[] words, String chars) {
        int count = 0;
        int sum = 0;
         final String preChar = chars;
         //将字符串数组遍历
        for (int i = 0; i < words.length; i++) {
        //将字符串遍历字符
            for (int j = 0; j < words[i].length(); j++) {
            //如果给定的字母表包含第i个字符串的第j个字符
                if (chars.contains(String.valueOf(words[i].charAt(j)))) {
                //重新组织字母表,因为题目中说明字母表的每一个字母只能用一次
                    chars = chars.substring(0, chars.indexOf(String.valueOf(words[i].charAt(j)))) + chars.substring(chars.indexOf(String.valueOf(words[i].charAt(j))) + 1);
                    //每次变化字母表,计数count加一
                    count++;
                } else {
                //如果不包含,就跳出循环
                    break;
                }
                //如果count等于字符串的第i个的长度(字符串变成字符数组的长度)
                if (count == words[i].length()) {
                //总和加上count,用来最后输出
                    sum += count;
                    break;
                }
            }
            //重置count,进行下一个字符串的操作
            count = 0;
            //字母表已经更新,去掉了使用过的字符
            chars = preChar;
        }
        return sum;
}

It really is what I do first algorithm problem, although the difficulty of definition is simple, but for a weak foundation for me, it is quite difficult

2, continuous array
successive arrays original title link

Given a binary array, found (length) containing the same number of sub-arrays 0 and 1 of the longest continuous.

Example 1:

Input: [0,1]
Output: 2
Description: [0, 1] having the same number of the longest consecutive sub-arrays 0 and 1.

Example 2:

Input: [0,1,0]
Output: 2
Description: [0, 1] (or [1, 0]) is a maximum continuous number of the same sub-arrays 0 and 1.

Note: The length of a given binary array of not more than 50,000.

Source: stay button (LeetCode)
this question is only one array means is 0 and 1, to find the maximum length of a considerable number of 0 to 1 and continuous subarray

 public int findMaxLength(int[] nums) {
     int max = 0;//初始化最大的长度
        int len = nums.length;
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        map.put(0, -1);//用来标识0和1
        //存放当前的最大值
        int sum = 0;
        //遍历数组nums
        for (int x = 0; x < len; x++) {
        //若当前的数是1
            if (nums[x] == 1) {
        //sum+1
                sum++;
            } else {
        //当前数是0就-1
                sum--;
            }
            //如果map的以sum为key的节点没有存放value
            if (map.get(sum) == null) {
            //添加value
                map.put(sum, x);
            } else {
            //否则当前所有的遍历的最大值是,之前最大值与当下遍历的下标与map中以sum为key的value的差值
                max = Math.max(max, x - map.get(sum));
            }
        }  
        return max; 
    }

First contact HashMap, from the next few days, has been very puzzled, HashMap dim ah, can store data, lists and arrays that can not do that today went to see HashMap Detailed source, sigh, really human smart, with the advantage of an array of linked lists can be combined, can add even thought the tree, really breathtakingly

3, letter word grouping ectopic
letter word grouping ectopic original title link

Given a string array, ectopic word letters together. Ectopic word letters refer to the same letters, but arranged in different strings.

Example:

Input: [ "eat", "tea ", "tan", "ate", "nat", "bat"],
Output:
[
[ "ATE", "EAT", "TEA"],
[ "NAT", "Tan"],
[ "BAT"]
]

Description:

All inputs are lowercase letters.
The order of the answers that were not considered.

Source: stay button (LeetCode)
of this title means the same letters in a linked list more than one word, the last more than a large list consisting of list

public List<List<String>> groupAnagrams(String[] strs) {
//这里的map泛型用了,String做key,ArrayList<String>做value,便于最后返回
           HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
           //遍历给的字符串数组
		for (String string : strs) {
		//将字符串变成char的数组
			char[] chars = string.toCharArray();
			//将它排序,目的为了筛选出相同字母组成的单词
			Arrays.sort(chars);
			//创建一个由字符数组组成的字符串的key
			String key = new String(chars);
			//如果map中不存在这个key
			if (!map.containsKey(key))
			//就put,并创建一个新的链表
				map.put(key, new ArrayList<String>());
			//map获取当前key的value也就是一个链表,在调用add将当前遍历的字符串放入链表
			map.get(key).add(string);
		}
		//返回值直接创建了一个链表,内容是map的value的集合,这里没有加泛型,其实加不加都可以
		return new ArrayList(map.values());
    }

The use of HashMap, feeling its usefulness is very powerful, highly efficient traversal, unrestricted key, value (defined when you can easily change), built-in high-efficiency method, as he hit call

I began to feel the charm of the algorithm, as much as possible after a thorough grasp a new problem every day

Be sure to protect their dreams, even to sacrifice everything.
--NANA

Published 13 original articles · won praise 8 · views 1082

Guess you like

Origin blog.csdn.net/qq_45000228/article/details/104228640