/* * @lc app=leetcode id=344 lang=rust * * [344] Reverse String * * https://leetcode.com/problems/reverse-string/description/ * * algorithms * Easy (65.83%) * Total Accepted: 615.8K * Total Submissions: 935.4K * Testcase Example: '["h","e","l","l","o"]' * * Write a function that reverses a string. The input string is given as an * array of characters char[]. * * Do not allocate extra space for another array, you must do this by modifying * the input array in-place with O(1) extra memory. * * You may assume all the characters consist of printable ascii * characters. * * * * * Example 1: * * * Input: ["h","e","l","l","o"] * Output: ["o","l","l","e","h"] * * * * Example 2: * * * Input: ["H","a","n","n","a","h"] * Output: ["h","a","n","n","a","H"] * * * * */ impl Solution { pub fn reverse_string(s: &mut Vec) { if s.is_empty() { return ;} let (mut i, mut j) = (0, s.len()-1); while i < j { s.swap(i, j); i += 1; j -= 1; } } } // pub structSolution; use std::collections::HashMap; use std::collections::HashSet; use std::fmt::Debug; use std::hash::Hash; use std::iter::FromIterator; // use std::collections::VecDeque; // use std::collections::BTreeMap; #[allow(dead_code)] pub fn print_map(map: &HashMap) { for (k, v) in map.iter() { println!("{:?}: {:?}", k, v); } } #[allow(dead_code)] pub fn say_vec(nums: Vec){ println!("{:?}", nums); } #[allow(dead_code)] pub fn char_frequency(s: String) -> HashMap { let mut res:HashMap = HashMap::new(); for c in s.chars(){ *res.entry(c).or_insert(0) += 1; } res } #[allow(dead_code)] pub fn vec_counter(arr: Vec) -> HashMap { let mut c = HashMap::new(); for n in arr { *c.entry(n).or_insert(0) += 1; } c } #[allow(dead_code)] pub fn vec_to_hashset(arr: Vec) -> HashSet { HashSet::from_iter(arr.iter().cloned()) } #[allow(dead_code)] pub fn int_to_char(n: i32) -> char { // Convert number 0 to a, 1 to b, ... assert!(n >= 0 && n <= 25); (n as u8 + 'a' as u8) as char }