From eb263a58e48b0918557f1b5120045753edaa6c15 Mon Sep 17 00:00:00 2001 From: IMC666 Date: Tue, 14 May 2019 21:57:18 +0800 Subject: [PATCH] Add files via upload --- Week_04/id_82/LeetCode_455_082.java | 19 +++++++++++++++++++ Week_04/id_82/LeetCode_720_082.java | 29 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 Week_04/id_82/LeetCode_455_082.java create mode 100644 Week_04/id_82/LeetCode_720_082.java diff --git a/Week_04/id_82/LeetCode_455_082.java b/Week_04/id_82/LeetCode_455_082.java new file mode 100644 index 00000000..1aa45332 --- /dev/null +++ b/Week_04/id_82/LeetCode_455_082.java @@ -0,0 +1,19 @@ +public int findContentChildren(int[] g, int[] s) { + int ret = 0; + + Arrays.sort(g); + Arrays.sort(s); + + int i = 0, j = 0; + while (i < g.length && j < s.length) { + if (g[i] <= s[j]) { + ret++; + i++; + j++; + }else if (g[i] > s[j]) { + j++; + } + } + + return ret; +} \ No newline at end of file diff --git a/Week_04/id_82/LeetCode_720_082.java b/Week_04/id_82/LeetCode_720_082.java new file mode 100644 index 00000000..0f3bf8e5 --- /dev/null +++ b/Week_04/id_82/LeetCode_720_082.java @@ -0,0 +1,29 @@ +class Solution { + public String longestWord(String[] words) { + //给定字符串数组,找出包含最长的字符串(包含其他单词的字母),如果存在相同长度,取字母排序小的 + //思路:对数组排序,再利用Set对字母存储,小的单词一定包含在后面大的单词里面。后面只需要取前缀相同的 + + Set set=new HashSet(); + for(int i=0;ilength||(words[i].length()==length&&words[i].compareTo(word)<0)){ + //如果存在相同长度的字符串,取字母排序较小的 + int len=words[i].length(); + while(len>0&&set.contains(words[i].substring(0,len))){ + //求相同的部分 + len--; + } + if(len==0){ + //说明该单词的所有字符串均为公共字母,将其标记为匹配串 + length=words[i].length(); + word=words[i]; + } + } + } + return word; + } +}