示例 2:
输入:s = “axc”, t = “ahbgdc”
输出:false
提示:
- 0 <= s.length <= 100
- 0 <= t.length <= 10^4
- 两个字符串都只由小写字符组成。
Python代码
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
"""
搜索s每个字符在t中第一个字符的下标i,每次搜索都缩小范围t[i:]
"""
len_s = len(s)
len_t = len(t)
if len_s > len_t:
return False
if len_s == len_t:
if s == t:
return True
else:
return False
index = 0
for i in range(len_s):
if s[i] in t[index:]:
# 更新下标
index = t[index:].index(s[i]) + 1 + index
else:
return False
else:
return True
if __name__ == '\_\_main\_\_':
s = Solution()
a = "abc"
b = "ahbgdc"
print(s.isSubsequence(a, b))
C++代码
#include <iostream>
using namespace std;
class Solution {
public:
bool isSubsequence(string s, string t) {
// size()获取字符的长度
int s_len = s.size();
int t_len = t.size();
int index = 0;
if(s_len > t_len) return false;
if(s_len == t_len){
if(s == t) return true;
else return false;
}
// substr()第一个参数是起始下标,第二个元素是长度,该函数是返回一个字符串的子串
// find()函数是找字符串中是否包含某个字符,若包含则返回下标,否则返回std::string::npos
for(int i = 0; i < s_len; i++){
if (t.substr(index, t_len - index).find(s[i]) != std::string::npos)
{
index = t.substr(index, t_len - index).find(s[i]) + 1 + index;
}
else
return false;
}
return true;
}
};
int main()
{
string a = "abc";
string b = "ahbgdc";
Solution s;
cout << s.isSubsequence(a, b) << endl;
return 0;
}
383. 赎金信
给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以,返回 true ;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。
示例 1:
输入:ransomNote = “a”, magazine = “b”
输出:false
示例 2:
输入:ransomNote = “aa”, magazine = “ab”
输出:false
示例 3:
输入:ransomNote = “aa”, magazine = “aab”
输出:true
提示:
- 1 <= ransomNote.length, magazine.length <= 10^5
- ransomNote 和 magazine 由小写英文字母组成
Python代码
class Solution(object):
def canConstruct(self, ransomNote, magazine):
"""
:type ransomNote: str
:type magazine: str
:rtype: bool
"""
# 思路:遍历ransomNote的元素c,每次删除magazine中的c。
# 若magazine长度 - ransomNote长度 = 删除后的magazine长度,则返回True
r_len = len(ransomNote)
m_len = len(magazine)
for i in range(r_len):
magazine = magazine.replace(ransomNote[i], "", 1) # 删除一个字符
if m_len - r_len == len(magazine):
return True
else:
return False
if __name__ == '\_\_main\_\_':
s = Solution()
ransomNote = "a"
magazine = "b"
print(s.canConstruct(ransomNote, magazine))
C++代码
#include <iostream>
using namespace std;
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int r_len = ransomNote.size();
int m_len = magazine.size();
for(int i = 0; i < r_len; i++){
if (magazine.find(ransomNote[i]) != std::string::npos)
{
int index = magazine.find(ransomNote[i]);
// erase()函数第一个参数是起始下标,第二个参数是要删除的长度,该函数用于删除字符串中的字串
magazine.erase(index, 1);
}
}
if (m_len - r_len == magazine.size()) return true;
else return false;
}
};
int main()
### 结尾
学习html5、css、javascript这些基础知识,学习的渠道很多,就不多说了,例如,一些其他的优秀博客。但是本人觉得看书也很必要,可以节省很多时间,常见的javascript的书,例如:javascript的高级程序设计,是每位前端工程师必不可少的一本书,边看边用,了解js的一些基本知识,基本上很全面了,如果有时间可以读一些,js性能相关的书籍,以及设计者模式,在实践中都会用的到。
**[开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】](https://bbs.csdn.net/topics/618166371)**
高级程序设计,是每位前端工程师必不可少的一本书,边看边用,了解js的一些基本知识,基本上很全面了,如果有时间可以读一些,js性能相关的书籍,以及设计者模式,在实践中都会用的到。
![html5](https://img-blog.csdnimg.cn/img_convert/d35c2cf2d5364b4907e65f1856c923a4.png)