Semi-discretization and full-discretization with optimal accuracy for charged-particle dynamics in a strong nonuniform magnetic field