🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 3 Min Lesezeit
0

LeetCode in Swift - 9. Palindrome Number

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




Problem











Abstract



To see a number is a palindrome number means it should be looked exactly the same with the origin when it got reversed.



For example: 121, 0



Additionally based on the Example 2, we need to consider negative numbers are not palindrome numbers.



Follow up message is solving the problem by trying not to convert to a string.






Solution - The String way



Code:




CODE
class Solution {
func isPalindrome(_ x: Int) -> Bool {
String(x) == String(String(x).reversed())
}
}






The workable way but not good for couple of reasons.




  • During string conversions consumed certain number of resources.

  • To able to be compared, the reversed string has to be converted back to a string.






Solution - Integer



Code:




CODE
class Solution {
func isPalindrome(_ x: Int) -> Bool {
// 1. Early exist
guard x >= 0 else { return false }

var origin = x
var reversed = 0

// 2. Routines
while origin != 0 {
reversed = reversed * 10 + origin % 10
origin = origin / 10
}

return reversed == x
}
}









Complexity




  • Time Complexity: O(n)



    • n is length(number of digits) of the given integer.






  • Space Complexity: O(1)


    • reversed and origin as 1 and 1 → O(2) → O(1)











1. Early exist



To eliminate the negative number, I added a guard here




CODE
guard x >= 0 else { return false }









2. Routines



To reverse an integer, during every routine, we can use the previous reversed number times 10, plus current remainder of x divides by 10, then divide the x by 10 and save back to x, do the same thing until the x becomes 0.



For example, when given is 121, steps can be shown as following




CODE
// Initial state
x = 121
r = 0 // short for reversed









CODE
// === 1st run ===
// x is 121, NOT 0, entering the while loop
// r = r * 10 + x % 10
r = 1
// x = x / 10
x = 12









CODE
// === 2nd run ===
// x is 12, NOT 0, entering the while loop
// r = r * 10 + x % 10
r = 12 // 1 * 10 + 2 = 12 // 2 is from 12 % 10 = 2
// x = x / 10
x = 1 // 12 / 10 = 1









CODE
// === 3rd run ===
// x is 1, NOT 0, entering the while loop
// r = r * 10 + x % 10
r = 121 // 12 * 10 + 1 = 12 // 1 is from 1 % 10 = 1
// x = x / 10
x = 0 // 1 / 10 = 0









CODE
// === 4th run ===
// x is 0, NOT entering the while loop
// === End of the routine ===






Since x is immutable as a method parameter, we need a mutable variable var origin = x to do the job for us.






Solution - Integer (2)






CODE
guard x >= 0 else { return false }






The precondition can be merged to the while loop's, but for readability, I am a little bit concern because the condition could became not so observable.



Another reason I am not happy with this is I have to declare origin and reversed for negative numbers.




CODE
class Solution {
func isPalindrome(_ x: Int) -> Bool {
var origin = x
var reversed = 0

while origin > 0 { // Changed from origin != 0 to origin > 0
reversed = reversed * 10 + origin % 10
origin = origin / 10
}

return reversed == x
}
}









Solution - Integer - Going half way



Code:




CODE
class Solution {
func isPalindrome(_ x: Int) -> Bool {
// 1. Early exist
guard x >= 0, !(x % 10 == 0 && x != 0) else { return false }

var origin = x
var reversed = 0

// 2. Routine
while origin > reversed {
reversed = reversed * 10 + origin % 10
origin /= 10
}

// 3. Evaluate
return origin == reversed || origin == reversed / 10
}
}









Thought



What if we can only go the half way, it can saving time when the given is an enormous integer.






2. Routine



When origin > reversed means the number already passed the middle point.






3. Evaluate




  • Determine by origin == reversed


    • When number of digits is even, eg.1221






  • Determine by origin == reversed / 10


    • When number of digits is odd, eg.121

    • To able to compare, needs to remove the number in the middle.

    • For example, Values of origin and reversed are 1 and 12.











End of post



That's it!



Please leave comment if you have any comments, thanks for your reading!

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten LeetCode in Swift - 9. Palindrome Number

Thematisch verwandte Begriffe: LeetCode, Swift, Palindrome, Number · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...