Skip to content

sgaliamov/leet-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LeetCode

This repository contains solutions to coding problems from LeetCode, implemented in Rust.
The goal is to practice problem-solving skills, improve Rust proficiency, and maintain high code quality.

Useful tricks

  1. Use String::as_bytes to efficiently access characters in a string.
  2. Use bitwise flags for tracking multiple boolean states:
    let mut flags = 0_u32;
    flags |= 1 << i; // Set flag i
    flags ^= 1 << i; // Clear flag i
  3. Use array indexing instead of HashMap for small fixed-size data sets to improve performance:
    let mut counts = [0; 26]; // For lowercase letters 'a' to 'z'
    for &b in s.into_bytes() {
         counts[(b - b'a') as usize] += 1;
    }
  4. Using linear search in small arrays is efficient enough and simple. No need for complex data structures.
  5. Use bitwise operations to count unique characters in a string:
    let mut mask = 0_u32;
    for &b in s.into_bytes() {
        mask |= 1 << (b - b'a');
    }
    let unique_count = mask.count_ones();
  6. Use faster hasher:
     let mut map: HashMap<_, _, _> =
         HashMap::with_capacity_and_hasher(26, BuildHasherDefault::<DefaultHasher>::default());

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published