zoukankan      html  css  js  c++  java
  • LeetCode刷题指南(字符串)

    作者:CYC2018

    文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7%A3.md

    本文主要介绍的是LeetCode题库中与字符串相关的经典题目,提供了LeetCode原题题号,参考答案,以及题目的部分解析。

    大家可以参考这个刷题指南来完成对字符串部分题目的练习,当然,这只是一部分,字符串的相关题目还有很多,譬如最长公共子序列和最长公共子串,这里列举的只是LeetCode中的字符串题目。

    字符串

    两个字符串包含的字符是否完全相同

    242. Valid Anagram (Easy)

    s = "anagram", t = "nagaram", return true.
    s = "rat", t = "car", return false.

    字符串只包含小写字符,总共有 26 个小写字符。可以用 HashMap 来映射字符与出现次数。因为键的范围很小,因此可以使用长度为 26 的整型数组对字符串出现的字符进行统计,然后比较两个字符串出现的字符数量是否相同。

    public boolean isAnagram(String s, String t) {    int[] cnts = new int[26];    for (char c : s.toCharArray()) {        cnts[c - 'a']++;    }    for (char c : t.toCharArray()) {        cnts[c - 'a']--;    }    for (int cnt : cnts) {        if (cnt != 0) {            return false;        }    }    return true;}

    计算一组字符集合可以组成的回文字符串的最大长度

    409. Longest Palindrome (Easy)

    Input : "abccccdd"
    Output : 7
    Explanation : One longest palindrome that can be built is "dccaccd", whose length is 7.

    使用长度为 256 的整型数组来统计每个字符出现的个数,每个字符有偶数个可以用来构成回文字符串。

    因为回文字符串最中间的那个字符可以单独出现,所以如果有单独的字符就把它放到最中间。

    public int longestPalindrome(String s) {    int[] cnts = new int[256];    for (char c : s.toCharArray()) {        cnts[c]++;    }    int palindrome = 0;    for (int cnt : cnts) {        palindrome += (cnt / 2) * 2;    }    if (palindrome < s.length()) {        palindrome++;   // 这个条件下 s 中一定有单个未使用的字符存在,可以把这个字符放到回文的最中间    }    return palindrome;}

    字符串同构

    205. Isomorphic Strings (Easy)

    Given "egg", "add", return true.
    Given "foo", "bar", return false.
    Given "paper", "title", return true.

    记录一个字符上次出现的位置,如果两个字符串中的字符上次出现的位置一样,那么就属于同构。

    public boolean isIsomorphic(String s, String t) {    int[] preIndexOfS = new int[256];    int[] preIndexOfT = new int[256];    for (int i = 0; i < s.length(); i++) {        char sc = s.charAt(i), tc = t.charAt(i);        if (preIndexOfS[sc] != preIndexOfT[tc]) {            return false;        }        preIndexOfS[sc] = i + 1;        preIndexOfT[tc] = i + 1;    }    return true;}

    回文子字符串

    647. Palindromic Substrings (Medium)

    Input: "aaa"
    Output: 6
    Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".

    从字符串的某一位开始,尝试着去扩展子字符串。

    private int cnt = 0;public int countSubstrings(String s) {    for (int i = 0; i < s.length(); i++) {        extendSubstrings(s, i, i);     // 奇数长度        extendSubstrings(s, i, i + 1); // 偶数长度    }    return cnt;}private void extendSubstrings(String s, int start, int end) {    while (start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)) {        start--;        end++;        cnt++;    }}

    判断一个整数是否是回文数

    9. Palindrome Number (Easy)

    要求不能使用额外空间,也就不能将整数转换为字符串进行判断。

    将整数分成左右两部分,右边那部分需要转置,然后判断这两部分是否相等。

    public boolean isPalindrome(int x) {    if (x == 0) {        return true;    }    if (x < 0 || x % 10 == 0) {        return false;    }    int right = 0;    while (x > right) {        right = right * 10 + x % 10;        x /= 10;    }    return x == right || x == right / 10;}

    统计二进制字符串中连续 1 和连续 0 数量相同的子字符串个数

    696. Count Binary Substrings (Easy)

    Input: "00110011"
    Output: 6
    Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01".



    public int countBinarySubstrings(String s) {int preLen = 0, curLen = 1, count = 0;    for (int i = 1; i < s.length(); i++) {        if (s.charAt(i) == s.charAt(i - 1)) {            curLen++;        } else {            preLen = curLen;            curLen = 1;        }        if (preLen >= curLen) {            count++;        }    }    return count;}

    字符串循环移位包含

    编程之美:3.1

    s1 = AABCD, s2 = CDAA
    Return : true

    给定两个字符串 s1 和 s2,要求判定 s2 是否能够被 s1 做循环移位得到的字符串包含。

    s1 进行循环移位的结果是 s1s1 的子字符串,因此只要判断 s2 是否是 s1s1 的子字符串即可。

    字符串循环移位

    编程之美:2.17

    s = "abcd123" k = 3
    Return "123abcd"

    将字符串向右循环移动 k 位。

    将 abcd123 中的 abcd 和 123 单独逆序,得到 dcba321,然后对整个字符串进行逆序,得到 123abcd。

    字符串中单词的翻转

    程序员代码面试指南

    s = "I am a student"
    return "student a am I"

    将每个单词逆序,然后将整个字符串逆序

    如果觉得文章不错,欢迎点好看和转发哈


    热 文 推 荐

    LeetCode刷题指南(数组和矩阵)

    长按扫码加关注,人在江湖不迷路

    1


    关注之后加星标,江湖要事早知道

  • 相关阅读:
    哪里错了
    任性
    【转】JVM 堆最大能调多大?
    【Android】PA4D_CH7 文件、保存状态和首选项
    【Android】PA4D_CH6 使用Internat资源
    Think In Java 多态学习
    【转】Myeclipse 查看Jar文件里面源码中文乱码 问题解决
    [转载]getBoundClientRect函数详解
    Kettle 环境变量设置
    SQL Server 2012 不支持创建连接服务器到 2000 版本,及其替代方案
  • 原文地址:https://www.cnblogs.com/xll1025/p/10441424.html
Copyright © 2011-2022 走看看