leetcode-22-string
521. Longest Uncommon Subsequence I
find the longest uncommon subsequence of this group of two strings
解题思路:
因为求的是最长uncommon subsequence的长度,所以,如果ab长度不等,应返回长度较大值;ab相同,则返回-1;否则返回长度即可。
int findLUSlength(string a, string b) {
if (a == b)
return -1;
if (a.size() != b.size())
return a.size() > b.size() ? a.size() : b.size();
return a.size();
}
392. Is Subsequence
Given a string s and a string t, check if s is subsequence of t.
解题思路:直接扫一遍t检查就好了。。注意,如果s已经找完,应该跳出循环,不然会runtime error
bool isSubsequence(string s, string t) {
int i, j;
for (i = 0, j = 0; i < t.length(); i++) {
if (j == s.length())
break;
if (t[i] == s[j])
j++;
}
return j == s.length();
}
541. Reverse String II
解题思路:
其实是以2k为一组,翻转前i个(i<=k)。所以对于长度为2k的正常翻转,最后一组考虑长度<k的情况。另外,对于k>s.length()时,要翻转
整个s。
string reverseStr(string s, int k) {
if (s.length() < 2 || k == 0 )
return s;
string result = s;
bool flag = false;
int i;
if (s.length() <= k) {
flag = true;
i = 0;
}
//int i;
if (flag == false) {
for (i = 0; i < s.length(); i = i + 2 * k) {
int m = i;
int n = i + k - 1;
if (n >= s.length()) {
flag = true;
break;
}
int temp;
while (m <= n) {
temp = result[m];
result[m] = result[n];
result[n] = temp;
m ++;
n --;
}
}
}
if (flag == true) {
int m = i;
int n = s.length()-1;
int temp;
while (m <= n) {
temp = result[m];
result[m] = result[n];
result[n] = temp;
m ++;
n --;
}
}
return result;
}
leetcode-22-string的更多相关文章
- LeetCode——Reverse String
LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- [LeetCode] Magical String 神奇字符串
A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...
- Leetcode:Scramble String 解题报告
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22. 括号生成(Generate Parentheses)
22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...
- Java实现 LeetCode 22 括号生成
22. 括号生成 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", &quo ...
- [LeetCode] Encode String with Shortest Length 最短长度编码字符串
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- [LeetCode] Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
随机推荐
- Oracle .NET Core
Oracle .NET Core Beta驱动已出,自己动手写EF Core Oracle https://www.cnblogs.com/yanweidie/p/9064609.html 使用.ne ...
- setState异步函数
changeLeader(value){ console.log(value) this.setState({ leader:value },() => { console.lo ...
- D. Statistics of Recompressing Videos
D. Statistics of Recompressing Videos time limit per test 3 seconds memory limit per test 256 megaby ...
- C#/.net基础知识
1 .NET 中类和结构的区别? 答:结构和类具有大体的语法,但是结构受到的限制比类要多.结构不能申明有默认的构造函数,为结构的副本是又编译器创建 和销毁的,所以不需要默认的构造函数和析构函数.结构 ...
- ArcGIS for Server 10.3.X 新型紧凑型缓存的解读和应用
早在2010年年底,牛魔王中王在其博客空间牛魔王的作坊中对ArcGIS 10中推出的紧凑型缓存格式进行了详细的解读,详见<ArcGIS 切片缓存紧凑文件格式分析与使用>.紧随着的4年时间里 ...
- countUp 动画展示数字变化
html <p id="countUp" style="font-size:25px;height:25px;background-color:#0aa;" ...
- redmine安装详解
1.Linux:centos6.4(32位)2.Gcc的编译环境.使用make命令编辑.yum install gcc-c++ 3.PCRE PCRE(Perl Compatible Regular ...
- sessionStorage 详解,特点,使用技巧,场景
很早之前久知道sessionStorage ,也学习过,但没有实战使用过 .最近团队遇到一个问题<electronjs中打开新页面sessionStorage丢失>,让我有机会重新来认识一 ...
- HDU 4741 Save Labman No.004 (几何)
题意:求空间两线的最短距离和最短线的交点 题解: 线性代数和空间几何,主要是用叉积,点积,几何. 知道两个方向向量s1,s2,求叉积可以得出他们的公共垂直向量,然后公共垂直向量gamma和两线上的点形 ...
- Adobe登陆出现Access denied解决方法
当我安装好Adobe Acrobat Reader DC时,想要登陆Adobe账号时被Access denied. 一般说需要梯子,然而本人亲测这种方法不行(香港主机,全局模式下). 一个简单有效的方 ...