C # exercises answer: zero and one [Difficulty:: Class 1] - view the C # programming classic exam, for you to challenge the basis of C # 1000 # 154 practice questions and other simple fun

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45444821/article/details/102737387

Simple fun # 154: zero and one [difficulty: Level 1]:

Answer 1:

namespace myjinxin
{
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    public class Kata
    {
public int ZeroAndOne(string s){
  return Regex.Replace(s,"10|01","").Length;
} 

    }
}

Answer 2:

namespace myjinxin
{
   using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

    public class Kata
    {
        public int ZeroAndOne(string s){    
         char[] ss = s.ToArray();
            int a = 0;
            for (int i = 0; i < ss.Length; i++)
            {
                if (i < ss.Length - 1&amp;&amp;ss[i] != ss[i + 1])
                {
                    i++;
                }
                else
                {
                    a++;
                }
            }
            return a;
        }
    }
}

Answer 3:

namespace myjinxin
{
  using System.Text.RegularExpressions;
  
  public class Kata
  {
    public int ZeroAndOne(string s)
    {
      return Regex.Replace(s, "10|01", "").Length;
    }
  }
}

Answer 4:

namespace myjinxin
{
    using System;
    using System.Linq;
    
    public class Kata
    {
        public int ZeroAndOne(string s)
        {
          string[] afterRemoving = s.Split(new string[]{"10","01"}, StringSplitOptions.RemoveEmptyEntries).ToArray();
         return String.Join("",afterRemoving).Length;          
        }
    }
}

Answer 5:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public int ZeroAndOne(string s){
          var length = 0;
            for (var i = 0; i < s.Length; i++)
            {
                if (i + 1 >= s.Length || s[i] == s[i + 1])
                    length++;
                else
                    i++;
            }
            return length;       
          
        }
    }
}

A6:

namespace myjinxin {
    using System.Linq;

    public class Kata {
        public int ZeroAndOne( string s ) {
            var a = s.ToCharArray( );
            var p = a [ 0 ];
            for ( int i = 1; i < a.Length; i++ ) {
                if ( ( p == '0' &amp;&amp; a [ i ] == '1' ) || ( p == '1' &amp;&amp; a [ i ] == '0' ) ) {
                    a [ i ] = '-';
                    a [ i - 1 ] = '-';
                }
                p = a [ i ];
            }
            return a.Where( c => c != '-' ).Count( );
        }
    }
}

A7:

namespace myjinxin
{
    using System;
    using System.Text.RegularExpressions;
    
    public class Kata
    {
        public int ZeroAndOne(string s){
          
          return Regex.Replace(s, "10|01", "").Length;
          
        }
    }
}

A8:

namespace myjinxin
{
    using System;
    using System.Text.RegularExpressions;
    
    public class Kata
    {
        public int ZeroAndOne(string s){
          return Regex.Replace(s, "01|10", "").Length;
        }
    }
}

A9:

namespace myjinxin {
    using System;
    
    public class Kata {
        public int ZeroAndOne(string s) {
            int newLen = 0;
            int oldLen = 0;
            do {
                oldLen = s.Length;
                for (int i = 0; i < s.Length - 1; i++) {
                    if ((s[i]=='0' &amp;&amp; s[i+1]=='1') || 
                        (s[i])=='1' &amp;&amp; s[i+1]=='0') {
                        s = s.Remove(i, 2);
                        s = s.Insert(i, " ");
                    }
                }
                newLen = s.Length;
            } while (newLen < oldLen);
            s = s.Replace(" ", "");
            return s.Length;
        }
    }
}

Answers 10:

namespace myjinxin
{
    using System;
    
    public class Kata
    {
        public int ZeroAndOne(string s)
        {    
          char[] temp = s.ToCharArray();   
          for(int i = 0; i < s.Length-1; i++)
          {
            if(temp[i] == '0' &amp;&amp; temp[i+1] == '1')
            {
              temp[i] = 'T'; temp[i+1] = 'T';
            } else if(temp[i] == '1' &amp;&amp; temp[i+1] == '0')
            {
              temp[i] = 'T'; temp[i+1] = 'T';
            }
          }
          
          return new string(temp).Replace("T","").Length; 
        }
    }
}




Guess you like

Origin blog.csdn.net/weixin_45444821/article/details/102737387