<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)392:判斷子序列

          共 2035字,需瀏覽 5分鐘

           ·

          2021-09-27 07:17

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 判斷子序列,我們先來看題面:
          https://leetcode-cn.com/problems/is-subsequence/

          Given two strings s and t, return true if s is a subsequence of t, or false otherwise.


          A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).


          給定字符串 s 和 t ,判斷 s 是否為 t 的子序列。
          字符串的一個(gè)子序列是原始字符串刪除一些(也可以不刪除)字符而不改變剩余字符相對(duì)位置形成的新字符串。(例如,"ace"是"abcde"的一個(gè)子序列,而"aec"不是)。
          進(jìn)階:
          如果有大量輸入的 S,稱作 S1, S2, ... , Sk 其中 k >= 10億,你需要依次檢查它們是否為 T 的子序列。在這種情況下,你會(huì)怎樣改變代碼?

          示例


          示例 1

          輸入:s = "abc", t = "ahbgdc"
          輸出:true

          示例 2

          輸入:s = "axc", t = "ahbgdc"
          輸出:false


          解題


          思路:
          從 0 開始遍歷字符串 t 與 s ,當(dāng)相等時(shí),s 的下標(biāo)右移;
          不管是否相等,t 都右移。

          class Solution {
              public boolean isSubsequence(String s, String t) {
                  int n = s.length(), m = t.length();
                  int i = 0, j = 0; //i -> s , j -> t
                  while(i < n && j < m){
                      if(s.charAt(i) == t.charAt(j)) i++; // 相等時(shí),i++
                      j++; // 不管是否相等,j++
                  }
                  return i == n ;
              }
          }



          好了,今天的文章就到這里,如果覺得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

          上期推文:

          LeetCode1-380題匯總,希望對(duì)你有點(diǎn)幫助!

          LeetCode刷題實(shí)戰(zhàn)381:O(1) 時(shí)間插入、刪除和獲取隨機(jī)元素

          LeetCode刷題實(shí)戰(zhàn)382:鏈表隨機(jī)節(jié)點(diǎn)

          LeetCode刷題實(shí)戰(zhàn)383:贖金信

          LeetCode刷題實(shí)戰(zhàn)384:打亂數(shù)組

          LeetCode刷題實(shí)戰(zhàn)385:迷你語(yǔ)法分析器

          LeetCode刷題實(shí)戰(zhàn)386:字典序排數(shù)
          LeetCode刷題實(shí)戰(zhàn)387:字符串中的第一個(gè)唯一字符
          LeetCode刷題實(shí)戰(zhàn)388:文件的最長(zhǎng)絕對(duì)路徑
          LeetCode刷題實(shí)戰(zhàn)389:找不同
          LeetCode刷題實(shí)戰(zhàn)390:消除游戲
          LeetCode刷題實(shí)戰(zhàn)391:完美矩形

          瀏覽 19
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日韩一页| 婷婷亚洲丁香 | 大吊一区二区三区 | 影音先锋在线成人视频 | 真的可以看 波多野结衣 一区 |