字符串篇(leetcode—最长公共前缀)

发布时间 2023-12-20 17:49:18作者: 吧拉吧拉吧

字符串

  • 百度百科:字符串或串(String)是由数字、字母、下划线组成的一串字符。一般记为 s="a1a2···an"(n>=0)。它是编程语言中表示文本的数据类型。
  • 常用函数
    • 比较函数
      • C++、Python等支持运算符重载的语言——可以使用 == 来比较两个字符串
      • JAVA等不支持运算符重载——可能无法使用 == 来比较两个字符串。当我们使用 == 时,它实际上会比较这两个对象是否是同一个对象。可以使用compareTo() 或者 equals()
        • compareTo函数比较两个字符串,并返回一个int类型。若字符串等于参数字符串,则返回0,字符串小于参数字符串,则返回值小于0;字符串大于参数字符串,返回值大于0。
        • equals比较内容是否相等,返回true或false
          String str1="abcd";
          String str2="abce";
          if(str1==str2){
            System.out.print("yes");
          }else{
            System.out.print("no");
          }
          // 结果输出yes
          String str3=new String("abcd");
          if(str1==str3){
            System.out.print("yes");
          }else{
            System.out.print("no");
          }
          // 结果输出no,str1和str3指向不同的对象
          
          //使用equal比较
          if(str1.equal(str3)){
            System.out.print("yes");
          }else{
            System.out.print("no");
          }
          // 结果输出yes
          
          //使用compareTo比较
          if(str1.compareTo(str3)==0){
            System.out.print("yes");
          }else{
            System.out.print("no");
          }
          // 结果输出yes
          
    • 获取字符串长度
      • JAVA 使用length() 方法——返回字符串对象包含的字符数
          String a = "hello";
          int len = a.length();
          System.out.println( "长度 : " + len );
          // 输出: 长度 : 5
        
      • C语言 使用sizeof()运算符 或者 strlen函数
        • sizeof 计算的是字符串在内存中的长度 即数组长度,返回的是变量声明后所占的内存数,而不是实际长度,即 '\0' 也计算到字符串长度中
        • strlen 计算的是字符串的有效长度,不包括 '\0'
    • 连接函数
      • JAVA中String 类提供了连接两个字符串的方法:
        • concat() 方法——返回 string2 连接 string1 的新字符串
            "hello ".concat("world");
          
        • 常用的是使用'+'操作符来连接字符串
            "hello "+"world";
          
      • C语言使用strcat()方法
        char str1[14] = "hello";
        char str2[14] = "world";
        strcat( str1, str2);
        printf("%s\n", str1 );
        // 输出:helloworld
        
  • 其他常见JAVA String类的方法
    • indexOf() 方法
      • public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
      • public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
      • int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
      • int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
    • substring() 方法
      • public String substring(int beginIndex)
      • public String substring(int beginIndex, int endIndex) —— beginIndex:起始索引(包括),索引从0开始;endIndex :结束索引(不包括)。
      • 以上都返回字符串的子字符串
            String Str = new String("helloworld");
            System.out.print("返回值 :" );
            System.out.println(Str.substring(5) );
            // 返回:world
            System.out.print("返回值 :" );
            System.out.println(Str.substring(0, 5) );
            // 返回:hello
        

例题:最长公共前缀(https://leetcode.cn/problems/longest-common-prefix/)

  • 分析:
    • 这个函数首先检查输入的字符串数组是否为空或长度为0,如果是,则返回空字符串。
    • 然后,它将第一个字符串作为初始前缀。
    • 然后,循环数组中的每个其他字符串,如果不以该前缀prefix开始,那么前缀就会被减少一个字符,直到找到一个前缀,所有的字符串都以它开始,或者前缀变为空字符串。如果前缀变为空字符串,那么函数返回空字符串。否则,函数返回找到的最长前缀。
  • 代码:
      class Solution {
        public String longestCommonPrefix(String[] strs) {
            if (strs == null || strs.length == 0) {  
                return "";  
            }  
              
            String prefix = strs[0];  
              
            for (int i = 1; i < strs.length; i++) {  
                while (strs[i].indexOf(prefix) != 0) {  
                    prefix = prefix.substring(0, prefix.length() - 1);  
                    if (prefix.isEmpty()) {  
                        return "";  
                    }  
                }  
            }  
              
            return prefix;  
        }
      }