ARTS-001

作者: 玖柒叁 | 来源:发表于2019-06-14 20:20 被阅读0次

Algorithm


175. Combine Two Tables

select FirstName, LastName, City, State
from Person left join Address
on Person.PersonId = Address.PersonId
;

PS:用as别名会失败;person可能没有地址,所以不能用内联

30. Substring with Concatenation of All Words

看到这道题目之后自己的思路:
假设字符串的长度是n,单词长度是b,共有m个单词
1,找出c个单词在字符串中的所有位置并记录。空间复杂度O(nm/b),时间复杂度O(nm)。【这个时间复杂度没有计入调用java的indexOf/substring方法的时间】
2,递归获取所有值。
上面的方法复杂度及其之高,实在是不可取。绞尽脑汁想了另外一个方法-----将所有值按顺序排序,然后再进行遍历

class Solution {
   public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> res = new ArrayList<Integer>();
        if (s == null || s.length() <= 0 || words == null || words.length <= 0) {
            return res;
        }
        int len = words[0].length();
        if (s.length() < len * words.length) {
            return res;
        }
        List<Node> nodeList = new ArrayList<Node>();
        for (int i = 0; i < words.length; ++i) {
            String word = words[i];
            int num = 0;
            for (int j = 0; j <= s.length() - len; ++j) {
                if (s.substring(j, j+len).equals(word)) {
                    Node node = new Node(i, j);
                    nodeList.add(node);
                    ++num;
                }
            }
            if (num <= 0) {
                return res;
            }
        }
        Collections.sort(nodeList, new Comparator<Node>() {
            public int compare(Node o1, Node o2) {
                return o1.strIndex-o2.strIndex;
            }
        });
        for (int i = 0; i <= nodeList.size() - words.length; ++i) {
            int[] flag = new int[words.length];
            int tp = words.length - 1;
            flag[nodeList.get(i).wordIndex] = 1;
            int last = nodeList.get(i).strIndex;
            for (int j = i+1; j < nodeList.size(); ++j) {
                if (flag[nodeList.get(j).wordIndex] == 1 || nodeList.get(j).strIndex != last + len) {
                    continue;
                }
                last = nodeList.get(j).strIndex;
                flag[nodeList.get(j).wordIndex] = 1;
                --tp;
            }
            if (tp == 0 && !res.contains(nodeList.get(i).strIndex)) {
                res.add(nodeList.get(i).strIndex);
            }
        }
        return res;
    }

    class Node {
        int wordIndex;
        int strIndex;

        public Node(int wordIndex, int strIndex) {
            this.wordIndex = wordIndex;
            this.strIndex = strIndex;
        }
    }
}

还是超时了,其实在寻找子串位置算法可以优化为KMP算法,不过我忘记这个算法的实现了。。。待我之后再去实现
只能看下别人是怎么解决这个问题的了。看了下这位大佬的解题思路:https://leetcode.com/problems/substring-with-concatenation-of-all-words/discuss/312488/Java-or-Without-extra-memory-or-80-or-With-comments
代码我就不贴了,可以点进链接自己看,我说下他的思路吧

  1. 每个单词的长度是wordLen
  2. 所有单词的总长度是conLen
  3. 单词个数为num
  4. 遍历字符串,从i=0开始获取长度为conLen的子串subStr
     遍历所有单词,从j=0,到j=num-1
      找到当前单词在子串subStr首次出现的位置idx(优化遍历)
      如果idx没有找到,break此次遍历,否则,将该单词从子串中去除,直到子串为空或遍历完所有单词,若单词都能在该子串找到,那么i就是我们需要找的

感觉并不复杂,但自己写的时候没想到,当然这种方法还不是最快的。有时间再看下其他人的解题思路

Review


https://www.javaworld.com/article/2073275/jax-rs-and-http-responses.html
读后小结:
这是一篇很早的文章,准确来说不是一篇技术文章,主要是从状态码返回和异常返回两方面介绍了JAX-RS的方便之处。

Tip


内联查询和外联查询。详见:https://www.jianshu.com/p/61f6140fd270

Share


https://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

后记
不写不知道,一写才发现,手写mysql真是渣渣,英语也是渣

相关文章

  • ARTS-001

    Algorithm 175. Combine Two Tables PS:用as别名会失败;person可能没有地...

网友评论

      本文标题:ARTS-001

      本文链接:https://www.haomeiwen.com/subject/ualmfctx.html