zoukankan      html  css  js  c++  java
  • LeetCode 392. Is Subsequence (判断子序列)

    题目标签:Greedy

      设两个 pointers,s_index  和  t_index;

      如果 s_index  和  t_index 位置上的字母一样,那么继续移动两个 pointers;

      如果字母不一样,只移动 t_index;

      具体看code。

    Java Solution:

    Runtime:  7 ms, faster than 80.12% 

    Memory Usage: 44.5 MB, less than 100.00%

    完成日期:02/19/2020

    关键点:two pointers

    class Solution {
        public boolean isSubsequence(String s, String t) {
            if(s.length() == 0) 
                return true;
            
            int s_index =0, t_index = 0;
            
            // use two pointers to find char in s and t
            while(t_index < t.length()) {
                if(s.charAt(s_index) == t.charAt(t_index)) { // if found the match, move s_index to next one
                    s_index++;
                    if(s_index == s.length())
                        return true;
                }
                t_index++;
            }
            return false;
        }
    }

    参考资料:LeetCode Discuss

    LeetCode 题目列表 - LeetCode Questions List

    题目来源:https://leetcode.com/

  • 相关阅读:
    (2).net体系
    (1)php开篇常识
    java基础知识-xx
    java基础知识-字符串与数组
    java基础知识-流程控制
    小明的喷漆计划
    加分二叉树
    括号序列
    P1045
    胖男孩
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/12334484.html
Copyright © 2011-2022 走看看