leetcode:fix illegal index and special cases bugs