2553. 分割数组中数字的数位
This commit is contained in:
parent
9452c4e658
commit
00bc762b5e
109
src/main.rs
109
src/main.rs
|
|
@ -1,105 +1,22 @@
|
||||||
mod arr;
|
mod arr;
|
||||||
|
|
||||||
struct Solution {}
|
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<Ordering> {
|
|
||||||
Some(self.cmp(other))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static mut IS_PRIME: Vec<Vec<i32>> = vec![];
|
|
||||||
|
|
||||||
pub fn get_is_prime() -> fn(i32) -> &'static Vec<i32> {
|
|
||||||
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<i32> {
|
|
||||||
unsafe { &IS_PRIME[x as usize] }
|
|
||||||
}
|
|
||||||
|
|
||||||
check
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn min_jumps(nums: Vec<i32>) -> i32 {
|
pub fn separate_digits(nums: Vec<i32>) -> Vec<i32> {
|
||||||
let mut arr = vec![1e6 as usize; nums.len()];
|
let mut ans = vec![];
|
||||||
let mut map = HashMap::<i32, Vec<usize>>::new();
|
for x in nums.iter() {
|
||||||
let is_prime = get_is_prime();
|
let mut y = *x;
|
||||||
for i in 0..nums.len() {
|
let mut local_ans = vec![];
|
||||||
if is_prime(nums[i]).len() == 2 {
|
while y > 0 {
|
||||||
map.entry(nums[i]).or_insert(vec![]).push(i);
|
local_ans.push(y % 10);
|
||||||
|
y /= 10;
|
||||||
}
|
}
|
||||||
|
local_ans.reverse();
|
||||||
|
ans.extend(local_ans.iter());
|
||||||
}
|
}
|
||||||
// key 跳数 val 下标
|
ans
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("{:?}", Solution::min_jumps(vec![5;1e5 as usize]));
|
println!("{:?}", Solution::separate_digits(vec![13, 25, 83, 77]));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue