当前位置:网站首页>LeetCode 02: 剑指 Offer 58 - I. 翻转单词顺序(简单); T123. 验证回文串 ; T9. 回文数
LeetCode 02: 剑指 Offer 58 - I. 翻转单词顺序(简单); T123. 验证回文串 ; T9. 回文数
2022-07-27 10:58:00 【无知小九】
文章目录
T4: 剑指 Offer 58 - I. 翻转单词顺序(简单)
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。为简单起见,标点符号和普通字母一样处理。例如输入字符串"I am a student. “,则输出"student. a am I”。
说明:
无空格字符构成一个单词。
输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/fan-zhuan-dan-ci-shun-xu-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
双指针法, 两个指针倒着, 遇到空格就跳过. 注意最后去除首位空格, 多用 while
解法:
class Solution {
public String reverseWords(String s) {
String tmp = s.trim();
int start = tmp.length() - 1;
int end = tmp.length() - 1;
String res = "";
while(start >= 0) {
while(start >= 0 && tmp.charAt(start) != ' ') {
start--;
}
res += tmp.substring(start + 1, end + 1) + " ";
while(start >= 0 && tmp.charAt(start) == ' ') {
start--;
}
end = start;
}
return res.trim();
}
}
T5: 125. 验证回文串 (简单)
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
说明:本题中,我们将空字符串定义为有效的回文串。
示例 1:
输入: "A man, a plan, a canal: Panama"
输出: true
示例 2:
输入: "race a car"
输出: false
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/valid-palindrome
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
把字符串反转对比或者用双指针判定
解法 1: 把字符串反转对比
class Solution {
public boolean isPalindrome(String s) {
StringBuffer sb = new StringBuffer();
for(int i = 0; i<s.length();++i){
if(Character.isLetterOrDigit(s.charAt(i))){
sb.append(Character.toLowerCase(s.charAt(i)));
}
}
StringBuffer sb2 = new StringBuffer(sb).reverse();
return sb.toString().equals(sb2.toString());
// 为什么这样错了
// StringBuffer sb2 = sb.reverse();
// return sb.toString().equals(sb2.toString());
}
}
执行用时:6 ms, 在所有 Java 提交中击败了**30.08%**的用户
内存消耗:38.6 MB, 在所有 Java 提交中击败了**37.37%**的用户
解法 2: 原地比较双指针
class Solution {
public boolean isPalindrome(String s) {
int n = s.length();
int left = 0, right = n - 1;
while (left < right) {
while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
++left;
}
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
--right;
}
if (left < right) {
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
return false;
}
++left;
--right;
}
}
return true;
}
}
执行用时:3 ms, 在所有 Java 提交中击败了**92.88%**的用户
内存消耗:38.3 MB, 在所有 Java 提交中击败了**87.85%**的用户
时间复杂度:O(|s|),其中 |s| 是字符串 s 的长度。
空间复杂度:O(1)。
T6: 9. 回文数(简单)
给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。
示例 1:
输入:x = 121
输出:true
示例 2:
输入:x = -121
输出:false
解释:从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
示例 3:
输入:x = 10
输出:false
解释:从右向左读, 为 01 。因此它不是一个回文数。
示例 4:
输入:x = -101
输出:false
提示:
- 231 <= x <= 231 - 1
进阶:你能不将整数转为字符串来解决这个问题吗?
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/palindrome-number
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
先把整数装换为字符串再双指针
解法: 双指针
class Solution {
public boolean isPalindrome(int x) {
if(x<0){
return false;
}
if(x>=0&&x<10){
return true;
}
String str = x + "";
int left = 0, right = str.length()-1;
while(left<right){
if(str.charAt(left)!=str.charAt(right)){
return false;
}
++left;
--right;
}
return true;
}
}
执行用时:18 ms, 在所有 Java 提交中击败了**10.15%**的用户
内存消耗:38.1 MB, 在所有 Java 提交中击败了**25.11%**的用户
边栏推荐
- Interval problem acwing 906. Interval grouping
- The C programming language (2nd) -- Notes -- 1.7
- Gaussian elimination acwing 883. solving linear equations with Gaussian elimination
- Longest ascending subsequence model acwing 1010. Interceptor missile
- Quantitative industry knowledge summary
- Force buckle - 10. Regular expression matching
- The C programming language (2nd) -- Notes -- 1.9
- Error while unzipping file in win10: unable to create symbolic link. You may need to run WinRAR with administrator privileges. The client does not have the required privileges
- Modelarts image classification and object detection
- C# 自定义集合
猜你喜欢

区间问题 AcWing 906. 区间分组

Maker harmony OS application development training notes 01

Digital triangle model acwing 1015. Picking flowers

Codeforces round #664C

【着色器实现Shake随机摇动效果_Shader效果第十篇】

C custom set

求组合数 AcWing 888. 求组合数 IV

Game theory acwing 893. Set Nim game

Moveit2 - 4. robot model and robot state

Find the combination number acwing 885. find the combination number I
随机推荐
局域网SDN技术硬核内幕 11 云网融合CP的关键——层次化端口绑定
Markdown editor syntax - setting of text color, size, font and background color (Reprint)
Win10 vscode code code format setting and remote breakpoint debugging
Kepserver configuration
1. Introduction and basic use of flume
Find the combinatorial number acwing 889. 01 sequence satisfying the condition
Everything cannot be searched for startup_ Lpc11x.s file
最长上升子序列模型 AcWing 1012. 友好城市
第12章 泛型
求组合数 AcWing 885. 求组合数 I
WGet warning: unable to verify
求组合数 AcWing 889. 满足条件的01序列
USB 网卡驱动数据流
求组合数 AcWing 887. 求组合数 III
(3) Pass parameters
Error encountered in adding quick open option to right-click menu:
Analysis of distributed database and cache double write consistency scheme (Reprint)
Quantitative industry knowledge summary
Digital triangle model acwing 1018. Minimum toll
Moveit2 - 5. Scenario Planning