Reverse Integer Problem - Leet Code + Rust

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 พ.ย. 2024

ความคิดเห็น • 14

  • @flyingsquirrel3271
    @flyingsquirrel3271 4 ปีที่แล้ว

    This approach definitely makes sense for teaching purposes. I managed to use a similar approach without using any allocation (no Strings, no Vecs). In Rust 1.46 it could even be implemented as a const function relatively easy. But if you're allocating anyway, you might as well collect into a String and use String::parse. Then the whole thing becomes a oneliner :D ->
    x.abs().to_string().chars().rev().collect::().parse::().map(|r| r * x.signum()).unwrap_or(0)

    • @danlogs
      @danlogs  4 ปีที่แล้ว

      Ooo, that's pretty cool! I'd love to see your other approach too ;D

  • @ManishSingh-ll4ws
    @ManishSingh-ll4ws 4 ปีที่แล้ว

    Hi, great tutorial, I have just started learning rust, through rust book and some of your tutorials, i am upto the 10th chapter an I don't think i have seen "10_i32" thing anywhere. Can you please explain or point me to some resource ?

    • @danlogs
      @danlogs  4 ปีที่แล้ว +1

      Thanks!
      To answer your question, "10_i32" is shorthand for "10 as i32". Without the explicit type, the compiler will complain with: "can't call method `pow` on ambiguous numeric type `{integer}`" ~ hope that clarifies it!

    • @mperez671
      @mperez671 4 ปีที่แล้ว +1

      You'll also sometimes see syntax like '10i32' or '10u8'

    • @ManishSingh-ll4ws
      @ManishSingh-ll4ws 4 ปีที่แล้ว

      @@danlogs thanks

  • @VanhPsyho
    @VanhPsyho 4 ปีที่แล้ว

    Only 480p available? Or is the video still being processed?

    • @danlogs
      @danlogs  4 ปีที่แล้ว +1

      Oof, thanks for bringing this to my attention. It should just be processing - Still, I’ll check back on this later to make sure 👌

    • @thisismyalias
      @thisismyalias 4 ปีที่แล้ว

      danlogs Still 480p for me too

    • @danlogs
      @danlogs  4 ปีที่แล้ว

      Upon further investigation, I think I might have messed up when exporting the video 🤦‍♂️ ~ I’m not sure if I can fix this as it is already uploaded but I’ll do my best to not make this mistake again and, while the quality is bad, I hope you can still enjoy the content :/

    • @danlogs
      @danlogs  4 ปีที่แล้ว +1

      Fixed in my latest upload: th-cam.com/video/wbCMbQ1rYXE/w-d-xo.html ✅

  • @ShkolnikPrahramist
    @ShkolnikPrahramist 4 ปีที่แล้ว

    fn reverse_integer(x: i32) -> i32 {
    let sign = if x >= 0 { 1 } else { -1 };
    let mut x = x * sign;
    let mut digit = x % 10;
    let mut result: i32 = 0;
    while x > 0 {
    result = result * 10 + digit;
    x = x / 10;
    digit = x % 10;
    }
    result * sign
    }

    • @danlogs
      @danlogs  4 ปีที่แล้ว

      Just don't forget to check for overflow! Try out values like 1534236469 when testing your program ^^

    • @ShkolnikPrahramist
      @ShkolnikPrahramist 3 ปีที่แล้ว +1

      @@danlogs if i pass value larger than i32 to function, compiler will bite my ass