Ad
Code
Diff
  • fn flip_the_number(x: &u64) -> u64 {
        let mut x = *x;
        let mut y = 0;
        while x != 0 {
            y = y * 10 + x % 10;
            x /= 10;
        }
        y
    }
    • fn flip_the_number(x: &u64) -> u64 {
    • x.to_string().chars().rev().collect::<String>().parse::<u64>().unwrap()
    • let mut x = *x;
    • let mut y = 0;
    • while x != 0 {
    • y = y * 10 + x % 10;
    • x /= 10;
    • }
    • y
    • }
Numbers
Data Types
Integers
Algorithms
Logic
Code
Diff
  • use std::collections::BTreeMap;
    
    fn digits(n: u64) -> usize {
        let powers = (0..20).map(|n| (
            if n == 0 { 0 } else { 10_u64.pow(n) },
            n as usize + 1
        )).collect::<BTreeMap<_, _>>();
        return *powers.range(..=n).last().unwrap().1;
    }
    • use std::collections::BTreeMap;
    • use std::ops::Bound::Unbounded;
    • use std::ops::Bound::Included;
    • fn digits(n: u64) -> usize {
    • let mut powers = BTreeMap::new();
    • powers.insert(0, 1);
    • powers.insert(10, 2);
    • powers.insert(100, 3);
    • powers.insert(1000, 4);
    • powers.insert(10000, 5);
    • powers.insert(100000, 6);
    • powers.insert(1000000, 7);
    • powers.insert(10000000, 8);
    • powers.insert(100000000, 9);
    • powers.insert(1000000000, 10);
    • powers.insert(10000000000, 11);
    • powers.insert(100000000000, 12);
    • powers.insert(1000000000000, 13);
    • powers.insert(10000000000000, 14);
    • powers.insert(100000000000000, 15);
    • powers.insert(1000000000000000, 16);
    • powers.insert(10000000000000000, 17);
    • powers.insert(100000000000000000, 18);
    • powers.insert(1000000000000000000, 19);
    • powers.insert(10000000000000000000, 20);
    • return *powers.range((Unbounded, Included(n))).last().unwrap().1;
    • let powers = (0..20).map(|n| (
    • if n == 0 { 0 } else { 10_u64.pow(n) },
    • n as usize + 1
    • )).collect::<BTreeMap<_, _>>();
    • return *powers.range(..=n).last().unwrap().1;
    • }
Code
Diff
  • fn foo() -> i32 {
        1
    }
    • fn foo() -> i32 {
    • 2
    • 1
    • }
Games
Arrays
Data Types
Algorithms
Logic
Code
Diff
  • fn required_energy(heights: Vec<i64>) -> i64 {
        heights
            .windows(2)
            .map(|y| (y[1] - y[0]).abs())
            .sum::<i64>()
    }
    • fn required_energy(heights: Vec<i64>) -> i64 {
    • heights
    • .iter()
    • .skip(1)
    • .zip(heights.iter())
    • .map(|(y2, y1)| (y2 - y1).abs())
    • .windows(2)
    • .map(|y| (y[1] - y[0]).abs())
    • .sum::<i64>()
    • }