-
Notifications
You must be signed in to change notification settings - Fork 64
Support display sup/sub style for numbers #12
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Thanks!!! 𝝺 pcs cargo run p 202
Finished dev [unoptimized + debuginfo] target(s) in 0.13s
Running `target/debug/leetcode p 202`
[202] Happy Number is on the run...
Write an algorithm to determine if a number is "happy".
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
--------------------------------------------------
Example:
Input: 19
Output: true
Explanation:
1² + 9² = 82
8² + 2² = 68
6² + 8² = 100
1² + 0² + 0² = 1
|
clearloop
reviewed
Mar 26, 2020
Comment on lines
+112
to
+138
pub fn superscript(n: u8) -> String { | ||
if n == 0 { | ||
"⁰".to_owned() | ||
} else if n == 1 { | ||
"¹".to_owned() | ||
} else if n == 2 { | ||
"²".to_owned() | ||
} else if n == 3 { | ||
"³".to_owned() | ||
} else if n == 4 { | ||
"⁴".to_owned() | ||
} else if n == 5 { | ||
"⁵".to_owned() | ||
} else if n == 6 { | ||
"⁶".to_owned() | ||
} else if n == 7 { | ||
"⁷".to_owned() | ||
} else if n == 8 { | ||
"⁸".to_owned() | ||
} else if n == 9 { | ||
"⁹".to_owned() | ||
} else if n >= 10 { | ||
superscript(n / 10) + &superscript(n % 10) | ||
} else { | ||
n.to_string() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can write like:
pub fn superscript(n: u8) -> String {
match n {
0 => "⁰".to_string(),
1 => "¹".to_string(),
2 => "²".to_string(),
3 => "³".to_string(),
4 => "⁴".to_string(),
5 => "⁵".to_string(),
6 => "⁶".to_string(),
7 => "⁷".to_string(),
8 => "⁸".to_string(),
9 => "⁹".to_string(),
x if x > 10 => (superscript(n / 10).parse().unwrap_or(0)
+ &superscript(n % 10).parse().unwrap_or(0))
.to_string(),
_ => n.to_string(),
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For example: leetcode problem 202