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;
    }


  

时间: 2024-11-08 08:08:14

leetcode-22-string的相关文章

LeetCode: Scramble String

LeetCode: Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble t

LeetCode: Interleaving String

LeetCode: Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc"

[leetcode]_Interleaving String

下午去蹭了一发新浪的笔试. 炒鸡多的网络基础知识,总共18道题,就写了8道左右吧,剩下的全是网络知识,这部分抽时间至少过一过. 其中一道算法题,回来跟嘟嘟商量,才发现是leetcode上的原题,连example都没有变,这可是道难度系数5的题,我嘞个去. 题目:给定三个字符串s1,s2,s3,判断s3是否能由s1和s2交错而成. 思路: 1.当s1当前字符 = s2当前字符 && s2当前字符 != s3当前字符 时,消s1. 2.同理状况 消s2. 3.难点在于当s1当前字符 = s2当

Leetcode: Encode String with Shortest Length &amp;&amp; G面经

Given a non-empty string, encode the string such that its encoded length is the shortest. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note: k will be a positive integ

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

Leetcode 数 String to Integer (atoi)

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie String to Integer (atoi) Total Accepted: 9862 Total Submissions: 67880 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,

[leetcode]Interleaving String @ Python

原题地址:https://oj.leetcode.com/problems/interleaving-string/ 题意: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac", return true.Whe

【LeetCode】- String to Integer (字符串转成整形)

[ 问题: ] Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes:It is intended for this problem to be specified vaguely (ie, no given input specs). Y

[LeetCode] Scramble String(树的问题最易用递归)

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choose a

[LeetCode] Interleaving String(dp)

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 分析:非常好的DP的训练题,