LeetCode Rust - 125 - 验证回文串

原题:125. 验证回文串 - 力扣(LeetCode)

解一,双指针

首尾添加双指针,找到两边第一个字母进行比较,相同则继续循环,否则返回 false

pub fn is_palindrome(s: String) -> bool {
	const A: i32 = 'A' as i32;
	const Z: i32 = 'Z' as i32;
	const a: i32 = 'a' as i32;
	const z: i32 = 'z' as i32;
	const zero: i32 = '0' as i32;
	const nine: i32 = '9' as i32;

	fn check_char(code: i32) -> bool {
		(code >= A && code <= Z) || (code >= a && code <= z) || (code >= zero && code <= nine)
	}

	let mut i = 0usize;
	let mut j = s.len() - 1;
	let chars: Vec<char> = s.chars().collect();
	while i < j {
		if !check_char(chars[i] as i32) {
			i += 1;
			continue;
		}
		if !check_char(chars[j] as i32) {
			j -= 1;
			continue;
		}
		if (!chars[i].to_lowercase().eq(chars[j].to_lowercase())) {
			return false;
		}
		i += 1;
		j -= 1;
	}
	return true;
}

时间:$O(n)$

$n$ 为字符串长度

空间:$O(1)$

反向链接


图谱

LeetCodeObsidian我的作品技术蓝色协议(日服)FLCL无职转生~到异世界就拿出真本事~混沌武士2023-06-062023-06-102023-06-132023-07-02Antd NoteBookCPU性能天梯图CSS NoteBookGFWIndexedDB 读写地狱JS 事件循环JS 原型链JS 运算符LaTeX NoteBookLeetCode Rust - 1 - 两数之和LeetCode Rust - 122 - 买卖股票的最佳时机 IILeetCode Rust - 125 - 验证回文串LeetCode Rust - 136 - 只出现一次的数字LeetCode Rust - 189 - 轮转数组LeetCode Rust - 217 - 存在重复元素LeetCode Rust - 242 - 有效的字母异位词LeetCode Rust - 26 - 删除有序数组中的重复项LeetCode Rust - 283 - 移动零LeetCode Rust - 344 - 反转字符串LeetCode Rust - 350 - 两个数组的交集 IILeetCode Rust - 36 - 有效的数独LeetCode Rust - 387 - 字符串中的第一个唯一字符LeetCode Rust - 48 - 旋转图像LeetCode Rust - 66 - 加一LeetCode Rust - 7 - 整数反转LeetCode Rust - 8 - 字符串转换整数 (atoi)Rust NoteBookYAMLcargo 配置hugo-obsidian 元数据解析问题safari 移动端适配发布同步国际邮箱安装 mingw显卡性能天梯图注册日本邮箱科学上网第三方插件配置 Ruby 环境🤖Gmail🤖Netch🤖Obsidian Git🤖Outlook🤖Plugin Proxy🤖epub.js🤖pdf.js🤖yuè - Web 阅读器🤖加速器HomeReact18 diff 算法Vue3 diff 算法Vue3 响应式系统Vue3 渲染流程CssMarkdownReactRustViteVueWebpack下载万代南梦宫启动器下载蓝色协议注册万代南梦宫账号🤖setup CliLeetcode originLeetcode time&space