diff --git a/src/main.rs b/src/main.rs index da55005..f4563bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,105 +1,22 @@ mod arr; + struct Solution {} - -use std::cmp::Ordering; -use std::collections::{BinaryHeap, HashMap}; - -#[derive(Eq, PartialEq)] -struct Item(usize, usize); // (键, 值) -impl Ord for Item { - fn cmp(&self, other: &Self) -> Ordering { - other.0.cmp(&self.0) // 反序,实现最小堆 - } -} - -impl PartialOrd for Item { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} - -static mut IS_PRIME: Vec> = vec![]; - -pub fn get_is_prime() -> fn(i32) -> &'static Vec { - unsafe { - #![allow(static_mut_refs)] - if IS_PRIME.is_empty() { - let n = 1e6 as usize; - IS_PRIME = vec![vec![]; n + 1]; - for i in 2..n + 1 { - IS_PRIME[i].push(1); - IS_PRIME[i].push(i as i32); - } - let mut primes = vec![0; n + 1]; - let mut cnt = 0; - - for i in 2..=n { - if IS_PRIME[i].len() == 2 - //是素数采用来筛 - { - cnt += 1; - primes[cnt] = i; //加入素数表 - for j in (2 * i..=n).step_by(i) { - IS_PRIME[j].push(i as i32); - } - } - } - } - } - - // 返回函数指针,访问时再用 unsafe - fn check(x: i32) -> &'static Vec { - unsafe { &IS_PRIME[x as usize] } - } - - check -} - impl Solution { - pub fn min_jumps(nums: Vec) -> i32 { - let mut arr = vec![1e6 as usize; nums.len()]; - let mut map = HashMap::>::new(); - let is_prime = get_is_prime(); - for i in 0..nums.len() { - if is_prime(nums[i]).len() == 2 { - map.entry(nums[i]).or_insert(vec![]).push(i); + pub fn separate_digits(nums: Vec) -> Vec { + let mut ans = vec![]; + for x in nums.iter() { + let mut y = *x; + let mut local_ans = vec![]; + while y > 0 { + local_ans.push(y % 10); + y /= 10; } + local_ans.reverse(); + ans.extend(local_ans.iter()); } - // key 跳数 val 下标 - let mut queue = BinaryHeap::new(); - queue.push(Item(0, nums.len() - 1)); - - arr[nums.len() - 1] = 0; - while let Some(Item(jump_count, index)) = queue.pop() { - if index == 0 { - return jump_count as i32; - } - if index > 0 { - if arr[index - 1] > arr[index] + 1 { - queue.push(Item(jump_count + 1, index - 1)); - arr[index - 1] = jump_count + 1; - } - } - if index < arr.len() - 1 { - if arr[index + 1] > arr[index] + 1 { - queue.push(Item(jump_count + 1, index + 1)); - arr[index + 1] = jump_count + 1; - } - } - - for i in is_prime(nums[index]).iter().skip(1) { - for x in map.entry(*i).or_default() { - if arr[*x] > arr[index] + 1 { - queue.push(Item(jump_count + 1, *x)); - arr[*x] = jump_count + 1; - } - } - map.remove_entry(i); - } - } - 0 + ans } } fn main() { - println!("{:?}", Solution::min_jumps(vec![5;1e5 as usize])); + println!("{:?}", Solution::separate_digits(vec![13, 25, 83, 77])); }