給一個長度爲n的字符串,怎麼實現循環右移k位,k可能大於n算法
使用undafe關鍵字,將string轉爲slice,在原slice上進行操做。編碼
use std::string::String; #[derive(Debug)] struct ShirftString { string :String, length : usize, } impl ShirftString { pub fn new(string:String)->ShirftString{ string.is_ascii(); let length = string.len(); ShirftString{ string, length, } } fn swap(&mut self,a:usize,b:usize){ assert!(a<self.length && b<self.length); unsafe{ let slice = self.string.as_bytes_mut(); slice.swap(a,b); } } fn get_swap_index(&self,index:usize,shift_amount:usize) -> usize{ let mut temp = (index+self.length-shift_amount)%self.length; while temp < index{ temp = (temp+self.length-shift_amount)%self.length } return temp; } pub fn right_shirft(&mut self,shift_amount:usize){ let _shift_amount = shift_amount%self.length; let mut swap_index:usize; for i in 0..self.length{ swap_index = self.get_swap_index(i,_shift_amount); self.swap(i,swap_index); } } pub fn get_string(&self)->&String{ &self.string } }
extern crate shirftstring; fn main() { let mut s = ShirftString::new("hello world".to_string()); s.right_shirft(5); println!("{}",s.get_string()); }
~blablabla~spa
> rust 不管是str仍是string:String類型均沒法經過索引進行讀取或修改;code
> 以上方案不支持除ASCII之外編碼的字符串索引