美文网首页
rust练习-5

rust练习-5

作者: robertzhai | 来源:发表于2022-07-24 08:11 被阅读0次

总结

二分查找,容易溢出 用 u64
String chars next 遍历,match匹配值
String into_bytes to vector
Self

1、https://leetcode.cn/problems/valid-perfect-square/submissions/

impl Solution {
    pub fn is_perfect_square(num: i32) -> bool {

        let mut l:u64 = 0;
        let target = num as u64;
        let mut r:u64 = target;
        let mut mid:u64;
        let mut tmp:u64;
        while l <= r {
            mid = ((r-l)>>1) + l;
            tmp = mid * mid;
            if tmp > target {
                r = mid - 1;
            } else if tmp < target {
                l = mid + 1;
            } else {
                return true;
            }

        }
        return false;

    }
}

2、https://leetcode.cn/problems/is-subsequence/submissions/

impl Solution {


    pub fn is_subsequence(s: String, t: String) -> bool {

        let (slen,tlen) = (s.len(), t.len());
        let (sbytes,tbytes) = (s.into_bytes(), t.into_bytes());

        let mut i :usize = 0;
        let mut j :usize = 0;
        while i < slen && j < tlen {
            if sbytes[i] == tbytes[j] {
                i+=1;
            }
            j +=1;
        }
        
        return i == slen;

    }

    pub fn is_subsequence_v2(s: String, t: String) -> bool {

        let mut tchars = t.chars();
        for c in s.chars() {
            loop {
                match tchars.next() {
                    Some(ct) => {
                        if c == ct {
                            break;
                        }
                    },
                    None => {
                        return false;
                    }
                }
            }
        }
        
        return true;

    }

    pub fn is_subsequence_v1(s: String, t: String) -> bool {

        let s_len = s.len() as usize;
        let mut idxs:usize = 0;
         let mut stack:Vec<char> = Vec::with_capacity(s_len);
         for c in s.chars() {
             stack.push(c);
         }
         for c in t.chars() {
            if idxs < s_len && c == stack[idxs] {
                idxs += 1;
            }
            
         }
        
        if idxs == s_len {
            return true;
        }
        return false;

    }
}

3、https://leetcode.cn/problems/range-sum-query-immutable/submissions/

struct NumArray {

    sum :Vec<i32>

}


/**
 * `&self` means the method takes an immutable reference.
 * If you need a mutable reference, change it to `&mut self` instead.
 */
impl NumArray {

    fn new(nums: Vec<i32>) -> Self {
        let total = nums.len() as usize;
        let mut data:Vec<i32> = Vec::with_capacity(total);
        let mut i = 1 as usize;
        data.push(nums[0]);
        while i < total {
            data.push(data[i-1] + nums[i]);
            i += 1;
        }
        Self {
            sum :data,
        }
    }
    
    fn sum_range(&self, left: i32, right: i32) -> i32 {
        
        if left == 0 {
            return self.sum[right as usize]
        }
        return self.sum[right as usize] - self.sum[left as usize - 1]
    }
}

/**
 * Your NumArray object will be instantiated and called as such:
 * let obj = NumArray::new(nums);
 * let ret_1: i32 = obj.sum_range(left, right);
 */

相关文章

  • rust练习-5

    总结 二分查找,容易溢出 用 u64String chars next 遍历,match匹配值String int...

  • Web3极客日报 #5

    Rust 编程小练习 Rustlings https://github.com/rust-lang/rustlin...

  • Rust语言编程实例100题-035

    Rust语言编程实例100题-035 题目:字符串反转练习,如将字符串 "i like rust!" 反转为"!t...

  • Rust语言编程实例100题-066

    Rust语言编程实例100题-066 题目:Rust指针练习。先来理解下引用和借用的概念。引用是作为参数传递给函数...

  • Rust101

    本篇文章将讲述如何在5分钟之内运行一个简单的Rust程序。 Rust语言(rust语言官方链接[https://w...

  • rust练习-1

    to be continued

  • rust练习-2

    总结 string 遍历,char比较vec 当做stack使用,出栈 入栈vec 变量 修改数据,usize 索...

  • rust练习-3

    总结 VecDequeVec reverseas 类型转换usize类型 -1 不会小于0,永远>=0双指针合并v...

  • rust练习-4

    总结 string的字符比较和遍历HashMap统计HashSet判重滑动窗口 1、https://leetcod...

  • Rust语言编程实例100题-059

    Rust语言编程实例100题-059 题目:Rust高阶函数练习。高阶函数是指以函数为参数或者返回值的函数,是函数...

网友评论

      本文标题:rust练习-5

      本文链接:https://www.haomeiwen.com/subject/rtcgirtx.html