当前位置: 首页 > news >正文

算法通关村第十二关—字符串转换(青铜)

一、转换成小写字母

LeetCode709.给你一个字符串s,将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。

示例1:
输入:s="Hello"
输出:"hello"
示例2:
输入:s="here"
输出:"here"
示例3:
输入:s="LOVELY"
输出:"lovely"

1.利用ASCII码转换

常见ASCII范围是:a-z:97-122 A-Z:65-90 0-9:48-57
当然,做题记不住的时候可以用ASCII码对应的字符表示

//此处用字符数组进行转换,也可以用StringBuffer
public static String toLowerCase(String s){
int n = s.length();
char[] chars = s.toCharArray();
for(int i = 0; i < n; ++i){
if(chars[i] >= 65 && chars[i] <= 90){//65可用'A'代替
chars[i] += 32;
}
String str = new String(chars);
return str;
}

2.利用字符串相关方法

toUpperCase(): 转换大小写,小变大
toLowerCase(): 转换大小写,大变小

class Solution {public String toLowerCase(String s) {return s.toLowerCase();}
}

二、字符串转换整数(atoi)

LeetCode8.本题的题目要求比较长,看原文:
image.png

public static int myAtoi(String str){
int len = str.length();
char[] charArray = str.toCharArray();
//1、去除前导空格
int index =0;
while(index len && charArray[index] == '') index++;//2、如果已经遍历完成(针对极端用例"     ")
if (index =len){
return 0;
}//3、如果出现符号字符,仅第1个有效,并记录正负
int sign = 1;
char firstChar = charArray [index];
if (firstChar =='+') index++;
else if (firstChar == '-'){
index++;
sign =-1;
}
//4、将后续出现的数字字符进行转换
//不能使用1ong类型,这是题目说的
int res = 0;
while(index < len){
char currChar = charArray[index];
//4.1先判断不合法的情况
if (currChar >'9' || currChar <'0') break;
//题目中说只能存储32位大小的有符号整数,下面两个1f分别处理整数和负数的情况。
//提前判断乘以10以后是否越界,但res*10可能会越界,所以这里使用Integer.MAX_VALUE / 10,这样一定不会越界
//这是解决溢出问题的经典处理方式
if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && (currChar - '0') > Integer.MAX_VALUE % 10)){
return Integer.MAX_VALUE;
}
if (res < Integer.MIN_VALUE / 10 || (res = Integer.MIN_VALUE / 10 && (currChar - '0') > -(Integer.MIN_VALUE % 10)){
return Integer.MIN_VALUE;
}
//合法的情况下,才考虑转换,每一步都把符号位乘进去
//想想这里为什么要带着sign乘
res = res * 10 + sign * (currChar -'0');
index++;
}
return res;
}
class Solution {public int myAtoi(String s) {StringBuffer str = new StringBuffer(s);while (str.length() > 0) {if (str.charAt(0) == ' ') str.delete(0, 1);else break;}if (str.length() == 0) return 0;int judge = 1;if (str.charAt(0) == '+') str.delete(0, 1);else if(str.charAt(0) == '-'){judge = -1;str.delete(0, 1);}int sum = 0;int max = Integer.MAX_VALUE;int min = Integer.MIN_VALUE;for(int n = 0; n < str.length(); n++){int a = (int)str.charAt(n) - '0';if(a >= 0 && a <= 9){if(judge == 1){if(sum > max / 10 || (sum == max / 10 && a > max % 10)) return max;else sum = sum * 10 + a;}if(judge == -1){if((-1) * sum  < min / 10 || ((-1) * sum == min / 10 && (-1)*a < min % 10)) return min;else sum = sum * 10 + a;}}else break;}return sum * judge;}
}
http://www.lryc.cn/news/260597.html

相关文章:

  • C#基础与进阶扩展合集-基础篇(持续更新)
  • ReactJs笔记摘录
  • 2023 re:Invent使用 PartyRock 和 Amazon Bedrock 安全高效构建 AI 应用程序
  • Mac 打不开github解决方案
  • 十五 动手学深度学习v2计算机视觉 ——全连接神经网络FCN
  • elementUI中的 “this.$confirm“ 基本用法,“this.$confirm“ 调换 “确认“、“取消“ 按钮的位置
  • K8S 常用命令
  • 12.使用 Redis 优化登陆模块
  • Nacos-NacosRule 负载均衡—设置集群使本地服务优先访问
  • 软件设计师——信息安全(二)
  • Unity中实现ShaderToy卡通火(原理实现篇)
  • 引迈信息-JNPF平台怎么样?值得入手吗?
  • 大数据云计算——使用Prometheus-Operator进行K8s集群监控
  • [蓝桥杯刷题]合并区间、最长不连续子序列、最长不重复数组长度
  • Hazel引擎学习(十二)
  • 中文字符串逆序输出
  • MySQL BinLog 数据还原恢复
  • 理想汽车校招内推--大量hc等你来
  • RabbitMQ死信队列详解
  • 计算机网络:物理层(编码与调制)
  • 嵌入式开发板qt gdb调试
  • 基于python实现原神那维莱特开转脚本
  • C# 实现Lru缓存
  • 牛客网BC107矩阵转置
  • 协作办公原来如此简单?详解 ONLYOFFICE 协作空间 2.0 更新
  • 2023年国赛高教杯数学建模A题定日镜场的优化设计解题全过程文档及程序
  • c/c++ 结构体、联合体、枚举
  • stl模板库成员函数重载类型混肴编译不通过解决方法
  • MySQL——表的约束
  • cordic 算法学习记录