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的更多相关文章

  1. LeetCode——Reverse String

    LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...

  2. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  3. [LeetCode] Magical String 神奇字符串

    A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...

  4. Leetcode:Scramble String 解题报告

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  5. [LeetCode] 22. Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  6. LeetCode 22. 括号生成(Generate Parentheses)

    22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...

  7. Java实现 LeetCode 22 括号生成

    22. 括号生成 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", &quo ...

  8. [LeetCode] Encode String with Shortest Length 最短长度编码字符串

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  9. [LeetCode] Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  10. [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 ...

随机推荐

  1. 关于EasyML的使用

    一.安装IntelliJ Idea 具体安装过程比较简单.但是遇到一个问题,如今LInux版本的IntelliJ的安装需要jdk1.8及以上版本的支持,但是EasyML目前仅支持jdk1.7的环境. ...

  2. 单个页面Request编码方式的改变,无需改动Web.config~

    搞一个东西,从别人的接口接一段中文,URL传输,怎么都有乱码~~ 得到对方的编码方式是gb2312,于是用HttpUtility.UrlDecode(_smssend_content, System. ...

  3. Java常用函数式接口--Consumer接口andThen()方法使用案例(二)

    Java常用函数式接口--Consumer接口使用案例

  4. Java中的日志框架

    日志框架的介绍和使用 常见的日志框架:JUL(Java.util.logging),JCL(jakarta commons logging),SLF4J,jboss-logging,Log4j,Log ...

  5. vue使用uglifyjs-webpack-plugin后打包报错

    楼主最新对已做项目进行打包优化,配置了打包环境下去除console.log语句插件---使用uglifyjs-webpack-plugin具体代码如下 npm install uglifyjs-web ...

  6. javascript结合nodejs实现多文件上传

    前端文件上传功能比较依赖后端,所以第一步用nodejs实现一个供文件上传的功能接口. 因为本人对nodejs也是一知半解,所以刚开始的想法是像原始的ajax交互那样,获取上传文件的内容,然后再通过no ...

  7. angularJS在移动端的点击事件延迟问题

    在运用angular开发移动端的应用时,发现它并没有将ng-click做兼容,在移动端使用ng-click事件仍然会有300ms延迟.后来发现angular有一个专门针对移动端的模块:angular- ...

  8. Got error 28 from storage engine的错误处理

    早上例行检查数据库,发现Got error 28 from storage engine这个错误,天那,我的数据.心里哇凉....备份的时间还是很久以前.最近更新了不少,麻烦大了. 好在找到了解决方法 ...

  9. UVA 1153 Keep the Customer Satisfied 顾客是上帝(贪心)

    因为每增加一个订单,时间是会增加的,所以先按截止时间d排序, 这样的话无论是删除一个订单,或者增加订单,都不会影响已经选好的订单. 然后维护一个已经选好的订单的大根堆(优先队列),如果当前无法选择的话 ...

  10. 【转】 树莓派初次启动攻略for Mac

    http://blog.csdn.net/rk2900/article/details/8632713/ 树莓派初次启动攻略for Mac made by Rk 感谢浙江大学<嵌入式系统> ...