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 ...
随机推荐
- NET中并行开发优化
NET中并行开发优化 让我们考虑一个简单的编程挑战:对大数组中的所有元素求和.现在可以通过使用并行性来轻松优化这一点,特别是对于具有数千或数百万个元素的巨大阵列,还有理由认为,并行处理时间应该与常规时 ...
- (转)通过MySQL复制线程SQL_Thread加快增量恢复binlog
数据回档常常是使用全量备份+binlog增量实现的.而数据量很大的情况下,增量恢复binlog一直是一个苦恼的问题,因为恢复binlog速度十分慢,并且容易出错. 恢复binlog文件一般有两种方法: ...
- 「干货分享」模块化编程和maven配置实践一则
封面 说到模块化编程,对我个人而言首先起因于团队协作的需要,也就是组织架构结构特点来决定,而不是跟风求得自我认同,看看我们团队的组织结构: 其中: 基础平台部职责: 1.AI实验室:语音,图像 ...
- NPOI读写Excel【转载】
参考示例:https://www.cnblogs.com/luxiaoxun/p/3374992.html 感谢! 1.整个Excel表格叫做工作表:WorkBook(工作薄),包含的叫页(工作表): ...
- Jquery each跳出循环
Jquery each跳出循环break--return false--跳出所有循环continue--return true--跳出当前循环
- 编译安装php容易出现的问题以及解决办法
http://crybit.com/20-common-php-compilation-errors-and-fix-unix/
- JavaBean+jsp开发模式 --结合form表单 实例
1.创建form表单 <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...
- HDFS读写策略
数据的读取过程: 数据读取: 客户端调用FileSystem 实例的open 方法,获得这个文件对应的输入流InputStream. 通过RPC 远程调用NameNode ,获得NameNode 中此 ...
- npm在linux即mac下更新时报错
nam在linux即mac下需要更新到新版本:
- TCP连接建立与关闭
http://hi.baidu.com/psorqkxcsfbbghd/item/70f3bd91943b9248f14215cd TCP连接建立与关闭 TCP 是一个面向连接的协议,无论哪一方向另一 ...