[leetcode]做题常用函数Tips

发布于 4 天前  6 次阅读


[leetcode]做题常用函数


  • 转换大小写:
    char a = std::toupper(c); // 转换为大写
    char a = std::tolower(c); // 转换为小写
    char a = Character.toUpperCase(c); // 转换为大写
    char a = Character.toLowerCase(c); // 转换为小写
  • 字符串反转:
    std::reverse(str.begin(), str.end()); // 反转字符串
    String reversed = new StringBuilder(str).reverse().toString(); // 反转字符串
  • 字符串拼接:
    String result = "Hello, " + "world!";//直接拼接
    StringBuffer sb = new StringBuffer();//通过StringBuffer拼接
    sb.append("Hello, ");
    sb.append("world!");
    String result = sb.toString();
    String result = String.format("%s, %s!", "Hello", "world");
    String result = String.join(", ", "Hello", "world");