My C# solution with one postion pointer


  • 0
    L
        public int Compress(char[] chars) {
            int liCount = 1;
            int liPos = -1;
            
            for( int i = 1; i <= chars.Length; ++i ) {
                if ( i == chars.Length || chars[i] != chars[i-1]) {
                    chars[++liPos] = chars[i-1];
                    if ( liCount > 1 ) {
                        foreach( char lchar in liCount.ToString() ) 
                            chars[++liPos] = lchar;
                        liCount = 1;
                    }
                } else {
                    ++ liCount;
                }
            }
            
            return liPos + 1;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.