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. java颜色代码对照表

    LightPink 浅粉色 #FFB6C1 255,182,193 Pink 粉红 #FFC0CB 255,192,203 Crimson 猩红 #DC143C 220,20,60 LavenderB ...

  2. Even-odd Boxes hackerrank 分类讨论

    https://www.hackerrank.com/contests/101hack50/challenges/even-and-odd-boxes/editorial 昨晚做的时候卡了挺久的. 首 ...

  3. WebStorm技巧-常用快捷键

      Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*-*/ ) Shift+F6 重构-重命名 Ctrl+X 删除行 Ctrl+D 复制行 Ctrl+G 查找行 Ctrl+Shift+ ...

  4. dubbo服务降级(1)

    1. 在 dubbo 管理控制台配置服务降级 上图的配置含义是:consumer 调用 com.zhang.HelloService 的方法时,直接返回 null,不发起远程调用. 实际操作是:在 z ...

  5. 零基础逆向工程23_PE结构07_重定位表_IAT表(待补充)

    重定位表 待补充 IAT表 待补充

  6. python之删除指定目录指定日期下的日志文件

    #=======================================================================================20190521以下脚本 ...

  7. 洛谷 P2380 狗哥采矿

    题目背景 又是一节平静的语文课 狗哥闲来无事,出来了这么一道题 题目描述 一个n*m的矩阵中,每个格子内有两种矿yeyenum和bloggium,并且知道它们在每个格子内的数量是多少.最北边有blog ...

  8. UVALive 5031 Graph and Queries (Treap)

    删除边的操作不容易实现,那么就先离线然后逆序来做. 逆序就变成了合并,用并存集判断连通,用Treap树来维护一个连通分量里的名次. Treap = Tree + Heap.用一个随机的优先级来平衡搜索 ...

  9. Android颜色选择器介绍

    使用Android的颜色选择器可以让我们的view在不同状态下显示不同的颜色. 1.Android中ListView 选择某项改变该行字体颜色 2.文件位置 res/color/filename.xm ...

  10. 得到本地应用程序的EXE的路径

    实现效果: 知识运用: Application类的ExecutablePath属性 //获取启动了应用程序的可执行文件的路径和可执行文件的名称    public static string Exec ...