zoukankan      html  css  js  c++  java
  • leetcode696

    本题先寻找字符串中0变1,或者1变0的位置作为分隔位置。然后从这个分隔位置同时向左、右两侧搜索。

    找到的左连续串和右连续串,都进行累计。

    public class Solution
        {
            public int CountBinarySubstrings(string s)
            {
                //s = "00110011";
                //这道题的思路是先找到分割点,然后用分割点,向两边同时搜索,找到连续串
                var len = s.Length;
                if (len < 2)
                {
                    return 0;
                }
                var sum = 0;
                var last = s[0].ToString();
    
                for (int i = 1; i < len; i++)
                {
                    var cur = s[i].ToString();
                    if (cur != last)
                    {
                        int a = i - 1;//向左
                        int b = i;//向右
                        while (a >= 0 && b <= len - 1)
                        {
                            var left = s[a].ToString();
                            var right = s[b].ToString();
                            if (last == left && cur == right)
                            {
                                sum++;
                                last = left;
                                cur = right;
                                a--;
                                b++;
                            }
                            else
                            {                            
                                break;
                            }
                        }  
                    }
                    last = s[i].ToString();
                }
    
                return sum;
            }
        }
  • 相关阅读:
    algorithm,ds,Skew heap
    python,file,os,shutil
    python,pickle,serialize
    algorithm,ds,leftist heap
    shell,chapter 2
    shell,chapter 1
    shell,Introduction to shell
    signal and slot in pyside
    python,cron,gae,bae
    python,mysql,sql,mysqldb
  • 原文地址:https://www.cnblogs.com/asenyang/p/9729093.html
Copyright © 2011-2022 走看看