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. Spring中的注入方式 和使用的注解 详解

    注解:http://www.cnblogs.com/liangxiaofeng/p/6390868.html 注入方式:http://www.cnblogs.com/java-class/p/4727 ...

  2. 使用表达式目录树实现SqlDataReader到实体的映射

    SqlDataReader映射实体,是ORM的基础功能,常见的实现方式有反射.表达式目录树和emit,这里要说的就是用表达式目录树生成实体的方法. 先分析下思路: 假设有个数据实体类,Student ...

  3. Jexus 5.8.2

    Jexus 5.8.2 正式发布为Asp.Net Core进入生产环境提供平台支持   Jexus 是一款运行于 Linux 平台,以支持  ASP.NET.PHP 为特色的集高安全性和高性能为一体的 ...

  4. 应用的入口——Startup

    应用的入口——Startup 一个ASP.NET Core应用被启动之后就具有了针对请求的处理能力,而这个能力是由管道赋予的,所以应用的启动同时意味着管道的成功构建.由于管道是由注册的服务器和若干中间 ...

  5. 《springcloud 五》springcloud stream

    什么是消息驱动? SpringCloud Stream消息驱动可以简化开发人员对消息中间件的使用复杂度,让系统开发人员更多尽力专注与核心业务逻辑的开发.SpringCloud Stream基于Spri ...

  6. Android sdk manager 显示 “Done loading packages”,该怎么办?

    试了这个方法:请用管理员的身份运行"SDK Manager.exe".不管用. 下面的方法可以: 在SDK Manager下Tools->Options打开了SDK Mana ...

  7. kie-api 组件介绍

    KieServices:kie整体的入口,可以用来创建Container,resource,fileSystem等 KieContainer: KieContainer就是一个KieBase的容器,可 ...

  8. Linux学习笔记——如何使用echo指令向文件写入内容

    0.前言     本文总结如何使用echo命令向文件中写入内容,例如使用echo指令覆盖文件内容,使用echo指令向文件追加内容,使用echo指令往文件中追加制表符.     echo向文件中输出内容 ...

  9. 介绍我最近做的网站 Asp.Net MVC4 + BootStrap

    一.前言 最近一直在做一个多站SEO数据分析的站点(www.easyyh.com),用了一些新技术,如Asp.Net MVC4,BootStrap,EasyUI,这些都是以前没有搞过的,最近搞得差不多 ...

  10. html-jquery/js引用外部图片时遇到看不了或出现403情况解决方法

    <script type="text/javascript"> function showImg(url) { var frameid = 'frameimg' + M ...