Skip to content

Commit

Permalink
feat: java for $125
Browse files Browse the repository at this point in the history
  • Loading branch information
azl397985856 authored Sep 23, 2022
2 parents 7736544 + bbfe977 commit 1458041
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion problems/125.valid-palindrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ https://leetcode-cn.com/problems/valid-palindrome/description/

## 代码

- 语言支持:JS,C++,Python
- 语言支持:JS,C++,Python,Java

JavaScript Code:

Expand Down Expand Up @@ -160,6 +160,33 @@ class Solution:
return s == s[::-1]
```

Java Code:

```java
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;
}
}
```

**复杂度分析**

- 时间复杂度:$O(N)$
Expand Down

0 comments on commit 1458041

Please sign in to comment.