面试题 10.05:稀疏数组搜索

This commit is contained in:
轩辕龙儿 2022-04-02 17:20:10 +08:00
parent 776b62b9b6
commit 14e31301e3
2 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,47 @@
//稀疏数组搜索有个排好序的字符串数组其中散布着一些空字符串编写一种方法找出给定字符串的位置
//
// 示例1:
//
// 输入: words = ["at", "", "", "", "ball", "", "", "car", "", "","dad", "", ""],
// s = "ta"
// 输出-1
// 说明: 不存在返回-1
//
//
// 示例2:
//
// 输入words = ["at", "", "", "", "ball", "", "", "car", "", "","dad", "", ""],
//s = "ball"
// 输出4
//
//
// 提示:
//
//
// words的长度在[1, 1000000]之间
//
// Related Topics 数组 字符串 二分查找 👍 66 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//面试题 10.05:稀疏数组搜索
public class SparseArraySearchLcci {
public static void main(String[] args) {
Solution solution = new SparseArraySearchLcci().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int findString(String[] words, String s) {
List<String> list = new ArrayList<>(Arrays.asList(words));
return list.indexOf(s);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,21 @@
<p>稀疏数组搜索。有个排好序的字符串数组,其中散布着一些空字符串,编写一种方法,找出给定字符串的位置。</p>
<p><strong>示例1:</strong></p>
<pre><strong> 输入</strong>: words = [&quot;at&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;ball&quot;, &quot;&quot;, &quot;&quot;, &quot;car&quot;, &quot;&quot;, &quot;&quot;,&quot;dad&quot;, &quot;&quot;, &quot;&quot;], s = &quot;ta&quot;
<strong> 输出</strong>-1
<strong> 说明</strong>: 不存在返回-1。
</pre>
<p><strong>示例2:</strong></p>
<pre><strong> 输入</strong>words = [&quot;at&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;ball&quot;, &quot;&quot;, &quot;&quot;, &quot;car&quot;, &quot;&quot;, &quot;&quot;,&quot;dad&quot;, &quot;&quot;, &quot;&quot;], s = &quot;ball&quot;
<strong> 输出</strong>4
</pre>
<p><strong>提示:</strong></p>
<ol>
<li>words的长度在[1, 1000000]之间</li>
</ol>
<div><div>Related Topics</div><div><li>数组</li><li>字符串</li><li>二分查找</li></div></div><br><div><li>👍 66</li><li>👎 0</li></div>