From 03a8cd27e4c2a3b5984c1cd2623e7ad8a65f3cd2 Mon Sep 17 00:00:00 2001 From: hanggegreat Date: Sat, 29 Jun 2019 12:29:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BAleetcode392=E9=A2=98=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=90=8E=E7=BB=AD=E6=8C=91=E6=88=98=E9=A2=98=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notes/Leetcode 题解 - 贪心思想.md | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/notes/Leetcode 题解 - 贪心思想.md b/notes/Leetcode 题解 - 贪心思想.md index 74e81249..a316822d 100644 --- a/notes/Leetcode 题解 - 贪心思想.md +++ b/notes/Leetcode 题解 - 贪心思想.md @@ -269,6 +269,48 @@ public boolean isSubsequence(String s, String t) { } ``` +后续挑战 : + +如果有大量输入的 S,称作S1, S2, ... , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码? + +对于这个后续挑战,因为有10亿以上的S,所以不能每次都去遍历T,而是应该把T的序列保存下来 + +定义一个dp数组,int [][]dp = new int[t.length()][26],其中dp[i][j] 表示T的i位置及之后,(j + 'a')对应的字符最早出现的位置 + +之后对S进行遍历,用tIndex记录T所到达的位置,并判断T的tIndex位置及之后是否有S当前位置元素,遍历完成返回true,如果在这期间tIndex越界,则返回false + +```java +public boolean isSubsequence(String s, String t) { + int [][]dp = new int[t.length()][26]; + int []pos = new int[26]; + for (int i = 0; i < 26; i++) { + pos[i] = t.length(); + } + + for (int i = dp.length - 1; i >= 0; i--) { + pos[t.charAt(i) - 'a'] = i; + for (int j = 0; j < 26; j++) { + dp[i][j] = pos[j]; + } + } + + int tIndex = 0; + for (int i = 0; i < s.length(); i++) { + if (tIndex >= t.length()) { + return false; + } + tIndex = dp[tIndex][s.charAt(i) - 'a']; + if (tIndex >= t.length()) { + return false; + } + tIndex++; + } + + return true; +} + +``` + # 9. 修改一个数成为非递减数组 [665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/)