当前位置: 首页>后端>正文

rust练习-5

总结

二分查找,容易溢出 用 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);
 */

https://www.xamrdz.com/backend/3vw1936681.html

相关文章: