Skip to content

Commit

Permalink
feat: 增加 9. 回文数
Browse files Browse the repository at this point in the history
  • Loading branch information
fxss5201 committed Jul 8, 2024
1 parent b989837 commit fe565f5
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default defineConfig({
{ text: '5. 最长回文子串', link: '/leetcode/longestPalindromicSubstring' },
{ text: '6. Z 字形变换', link: '/leetcode/zigzagConversion' },
{ text: '7. 整数反转', link: '/leetcode/reverseInteger' },
{ text: '9. 回文数', link: '/leetcode/palindromeNumber' },
{ text: '83. 删除排序链表中的重复元素', link: '/leetcode/removeDuplicatesFromSortedList' },
{ text: '2085. 统计出现过一次的公共字符串', link: '/leetcode/countCommonWordsWithOneOccurrence' },
{ text: '2182. 构造限制重复的字符串', link: '/leetcode/constructStringWithRepeatLimit' },
Expand Down
12 changes: 12 additions & 0 deletions docs/leetcode/palindromeNumber.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 9. 回文数

[9. 回文数](https://leetcode.cn/problems/palindrome-number/)

## 代码

::: code-group

<<< ../../src/leetcode/palindromeNumber/javascript.js{javascript} [javascript]
<<< ../../src/leetcode/palindromeNumber/typescript.ts{typescript} [typescript]

:::
2 changes: 1 addition & 1 deletion docs/leetcode/reverseInteger.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
<<< ../../src/leetcode/reverseInteger/javascript.js{javascript} [javascript]
<<< ../../src/leetcode/reverseInteger/typescript.ts{typescript} [typescript]

:::
:::
10 changes: 10 additions & 0 deletions src/leetcode/palindromeNumber/javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function isPalindrome(x) {
if (x < 0) return false
let num = x
let res = 0
while (num !== 0) {
res = res * 10 + num % 10
num = ~~(num / 10)
}
return res === x
}
10 changes: 10 additions & 0 deletions src/leetcode/palindromeNumber/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function isPalindrome(x: number): boolean {
if (x < 0) return false
let num = x
let res = 0
while (num !== 0) {
res = res * 10 + num % 10
num = ~~(num / 10)
}
return res === x
}
7 changes: 7 additions & 0 deletions test/palindromeNumber.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from 'vitest'
import { isPalindrome } from '../src/leetcode/palindromeNumber/typescript.ts'


test(`121`, () => {
expect(isPalindrome(121)).toEqual(true)
})
2 changes: 1 addition & 1 deletion test/reverseInteger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { reverse } from '../src/leetcode/reverseInteger/typescript.ts'


test(`123`, () => {
expect(reverse(123)).toEqual(321)
expect(reverse(123)).toBe(321)
})

0 comments on commit fe565f5

Please sign in to comment.