|
@@ -0,0 +1,51 @@
|
|
|
|
+struct Solution;
|
|
|
|
+
|
|
|
|
+impl Solution {
|
|
|
|
+ pub fn convert_to_title(column_number: i32) -> String {
|
|
|
|
+ let mut v = vec![];
|
|
|
|
+ let mut residual = column_number;
|
|
|
|
+ while residual != 0 {
|
|
|
|
+ let tmp = if residual % 26 == 0 { 26 } else { residual % 26 };
|
|
|
|
+ v.push(tmp);
|
|
|
|
+
|
|
|
|
+ residual -= tmp;
|
|
|
|
+ residual /= 26;
|
|
|
|
+ }
|
|
|
|
+ // println!("v: {:?}", v);
|
|
|
|
+ v.iter().rev().map(|&x| Self::i_to_a(x)).collect()
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fn i_to_a(num: i32) -> char {
|
|
|
|
+ ('A' as u8 + num as u8 - 1) as char
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#[cfg(test)]
|
|
|
|
+mod tests {
|
|
|
|
+ use super::*;
|
|
|
|
+
|
|
|
|
+ #[test]
|
|
|
|
+ fn test_convert_to_title() {
|
|
|
|
+ let column_number = 701;
|
|
|
|
+ let res =Solution::convert_to_title(column_number);
|
|
|
|
+ assert_eq!(res, String::from("ZY"));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ #[test]
|
|
|
|
+ fn test_convert_to_title2() {
|
|
|
|
+ let column_number = 28;
|
|
|
|
+ let res =Solution::convert_to_title(column_number);
|
|
|
|
+ assert_eq!(res, String::from("AB"));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ #[test]
|
|
|
|
+ fn test_i_to_a() {
|
|
|
|
+ let num = 1;
|
|
|
|
+ let res = Solution::i_to_a(num);
|
|
|
|
+ assert_eq!(res, 'A');
|
|
|
|
+ }
|
|
|
|
+}
|