Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.0090 #4021

Merged
merged 3 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions solution/0000-0099/0009.Palindrome Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,12 @@ function isPalindrome(x: number): boolean {
```rust
impl Solution {
pub fn is_palindrome(mut x: i32) -> bool {
if x < 0 || (x % 10 == 0 && x != 0) {
if x < 0 || (x != 0 && x % 10 == 0) {
return false;
}
let mut y = 0;
while x > y {
y *= 10;
y += x % 10;
y = y * 10 + x % 10;
x /= 10;
}
x == y || x == y / 10
Expand Down Expand Up @@ -213,17 +212,13 @@ var isPalindrome = function (x) {
#### C#

```cs
public class Solution
{
public bool IsPalindrome(int x)
{
if (x < 0 || (x > 0 && x % 10 == 0))
{
public class Solution {
public bool IsPalindrome(int x) {
if (x < 0 || (x > 0 && x % 10 == 0)) {
return false;
}
int y = 0;
for (; y < x; x /= 10)
{
for (; y < x; x /= 10) {
y = y * 10 + x % 10;
}
return x == y || x == y / 10;
Expand All @@ -236,14 +231,19 @@ public class Solution
```php
class Solution {
/**
* @param int $x
* @return boolean
* @param Integer $x
* @return Boolean
*/

function isPalindrome($x) {
$str = (string) $x;
$str_reverse = strrev($str);
return $str === $str_reverse;
if ($x < 0 || ($x && $x % 10 == 0)) {
return false;
}
$y = 0;
while ($x > $y) {
$y = $y * 10 + ($x % 10);
$x = (int) ($x / 10);
}
return $x == $y || $x == (int) ($y / 10);
}
}
```
Expand Down
34 changes: 17 additions & 17 deletions solution/0000-0099/0009.Palindrome Number/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,12 @@ function isPalindrome(x: number): boolean {
```rust
impl Solution {
pub fn is_palindrome(mut x: i32) -> bool {
if x < 0 || (x % 10 == 0 && x != 0) {
if x < 0 || (x != 0 && x % 10 == 0) {
return false;
}
let mut y = 0;
while x > y {
y *= 10;
y += x % 10;
y = y * 10 + x % 10;
x /= 10;
}
x == y || x == y / 10
Expand Down Expand Up @@ -205,17 +204,13 @@ var isPalindrome = function (x) {
#### C#

```cs
public class Solution
{
public bool IsPalindrome(int x)
{
if (x < 0 || (x > 0 && x % 10 == 0))
{
public class Solution {
public bool IsPalindrome(int x) {
if (x < 0 || (x > 0 && x % 10 == 0)) {
return false;
}
int y = 0;
for (; y < x; x /= 10)
{
for (; y < x; x /= 10) {
y = y * 10 + x % 10;
}
return x == y || x == y / 10;
Expand All @@ -228,14 +223,19 @@ public class Solution
```php
class Solution {
/**
* @param int $x
* @return boolean
* @param Integer $x
* @return Boolean
*/

function isPalindrome($x) {
$str = (string) $x;
$str_reverse = strrev($str);
return $str === $str_reverse;
if ($x < 0 || ($x && $x % 10 == 0)) {
return false;
}
$y = 0;
while ($x > $y) {
$y = $y * 10 + ($x % 10);
$x = (int) ($x / 10);
}
return $x == $y || $x == (int) ($y / 10);
}
}
```
Expand Down
12 changes: 4 additions & 8 deletions solution/0000-0099/0009.Palindrome Number/Solution.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
public class Solution
{
public bool IsPalindrome(int x)
{
if (x < 0 || (x > 0 && x % 10 == 0))
{
public class Solution {
public bool IsPalindrome(int x) {
if (x < 0 || (x > 0 && x % 10 == 0)) {
return false;
}
int y = 0;
for (; y < x; x /= 10)
{
for (; y < x; x /= 10) {
y = y * 10 + x % 10;
}
return x == y || x == y / 10;
Expand Down
17 changes: 11 additions & 6 deletions solution/0000-0099/0009.Palindrome Number/Solution.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
class Solution {
/**
* @param int $x
* @return boolean
* @param Integer $x
* @return Boolean
*/

function isPalindrome($x) {
$str = (string) $x;
$str_reverse = strrev($str);
return $str === $str_reverse;
if ($x < 0 || ($x && $x % 10 == 0)) {
return false;
}
$y = 0;
while ($x > $y) {
$y = $y * 10 + ($x % 10);
$x = (int) ($x / 10);
}
return $x == $y || $x == (int) ($y / 10);
}
}
21 changes: 7 additions & 14 deletions solution/0000-0099/0009.Palindrome Number/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
impl Solution {
pub fn is_palindrome(x: i32) -> bool {
if x < 0 {
pub fn is_palindrome(mut x: i32) -> bool {
if x < 0 || (x != 0 && x % 10 == 0) {
return false;
}
let s = x.to_string();
let bs = s.as_bytes();
let n = bs.len();
let mut l = 0;
let mut r = n - 1;
while l < r {
if bs[l] != bs[r] {
return false;
}
l += 1;
r -= 1;
let mut y = 0;
while x > y {
y = y * 10 + x % 10;
x /= 10;
}
true
x == y || x == y / 10
}
}
Loading