zoukankan      html  css  js  c++  java
  • 524. Longest Word in Dictionary through Deleting(通过删除找到字典中最长的单词)(leetcode)

    Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.

    Example 1:

    Input:
    s = "abpcplea", d = ["ale","apple","monkey","plea"]
    
    Output: 
    "apple"
    

    Example 2:

    Input:
    s = "abpcplea", d = ["a","b","c"]
    
    Output: 
    "a"
    

    Note:

      1. All the strings in the input will only contain lower-case letters.
      2. The size of the dictionary won't exceed 1,000.
      3. The length of all the strings in the input won't exceed 1,000.

    题目描述:删除 s 中的一些字符,使得它构成字符串列表 d 中的一个字符串,找出能构成的最长字符串。如果有多个相同长度的结果,返回字典序的最小字符串。

    分析:

    1、先在字典中找到符合的。判断是否是最长字符串,然后再判断是否有相同长度的结果,返回字典序的最小字符串。

    2、字典:字典其实就是个集合。

    遍历map集合的方法。

    public class TestMap {
        public static void main(String[] args) {
            Map<Integer, String> map = new HashMap<Integer, String>();
            map.put(1, "a");
            map.put(2, "b");
            map.put(3, "ab");
            map.put(4, "ab");
            map.put(4, "ab");// 和上面相同 , 会自己筛选
            System.out.println(map.size());
            // 第一种:
            /*
             * Set<Integer> set = map.keySet(); //得到所有key的集合
             *
             * for (Integer in : set) { String str = map.get(in);
             * System.out.println(in + "     " + str); }
             */
            System.out.println("第一种:通过Map.keySet遍历key和value:");
            for (Integer in : map.keySet()) {
                //map.keySet()返回的是所有key的值
                String str = map.get(in);//得到每个key多对用value的值
                System.out.println(in + "     " + str);
            }
            // 第二种:
            System.out.println("第二种:通过Map.entrySet使用iterator遍历key和value:");
            Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                 Map.Entry<Integer, String> entry = it.next();
                   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
            }
            // 第三种:推荐,尤其是容量大时
            System.out.println("第三种:通过Map.entrySet遍历key和value");
            for (Map.Entry<Integer, String> entry : map.entrySet()) {
                //Map.entry<Integer,String> 映射项(键-值对)  有几个方法:用上面的名字entry
                //entry.getKey() ;entry.getValue(); entry.setValue();
                //map.entrySet()  返回此映射中包含的映射关系的 Set视图。
                System.out.println("key= " + entry.getKey() + " and value= "
                        + entry.getValue());
            }
            // 第四种:
            System.out.println("第四种:通过Map.values()遍历所有的value,但不能遍历key");
            for (String v : map.values()) {
                System.out.println("value= " + v);
            }
        }

    时间复杂度:o(n^2)                 运行时间:38 ms            占用内存:39.9

    苟有恒,何必三更眠五更起;最无益,莫过一日暴十日寒。
  • 相关阅读:
    浅谈Android系统开发中LOG的使用
    在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
    美国地名大全(美国城市名称英文、中文)
    简单工厂模式
    Union和Union All的差别
    strlen和sizeof的差别
    css+div网页设计(一)--基础知识
    Java程序猿面试题集(181- 199)
    【Android XMPP】 学习资料收集贴(持续更新)
    curl命令具体解释
  • 原文地址:https://www.cnblogs.com/shaer/p/10423116.html
Copyright © 2011-2022 走看看