zoukankan      html  css  js  c++  java
  • LintCode #3 统计数字

    解题思路请参考

    代码(可以通过,不过很乱,需要整理):

    /// <summary>
            /// 计算n在数组[targetNum]中出现的次数
            /// 形如:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
            /// </summary>
            /// <param name="targetNum"></param>
            /// <param name="n"></param>
            /// <returns></returns>
            public static int GetCount(int targetNum, int n)
            {
                int count = 0;
    
                if (n > 0 && n <= 9)
                {
                    int ratio = 10;
    
                    while (true)
                    {
                        int tmp = targetNum/ratio * ratio;
    
                        if (tmp > 0)
                        {
                            count += tmp / 10;
                        }
    
                        int tmpA = targetNum - tmp;
    
                        if (ratio == 10)
                        {
                            if (tmpA >= n)
                            {
                                count++;
                            }
                        }
                        else
                        {
                            int tmpB = tmpA/(ratio/10);
                            if (tmpB > n)
                            {
                                count += 1*(ratio/10);
                            }
                            else if (tmpB == n)
                            {
                                count += tmpA%(ratio/10) + 1;
                            }
                        }
                        ratio *= 10;
    
                        if (tmp == 0)
                        {
                            break;
                        }
                    }
                }
                else if (n == 0)
                {
                    int ratio = 10;
    
                    while (true)
                    {
                        int tmp = targetNum / ratio * ratio;
    
                        if (tmp > 0)
                        {
                            count += tmp / 10;
                        }
    
                        
                        int tmpA = targetNum - tmp;
                        if (tmpA == targetNum)
                        {
                            if (tmpA > 100)
                            {
                                count -= ratio/10/10;
                            }
                            else if (ratio == 10)
                            {
                                count = 1;
                            }
                            break;
                        }
                        if (ratio == 10)
                        {
                            if (tmpA >= n)
                            {
                                count++;
                            }
                        }
                        else
                        {
                            int tmpB = tmpA / (ratio / 10);
                            if (tmpB > n)
                            {
                                count += 1 * (ratio / 10);
                            }
                            else if (tmpB == n)
                            {
                                count += tmpA % (ratio / 10) + 1;
                            }
                        }
                        ratio *= 10;
                    }
                }
                return count;
            }
  • 相关阅读:
    Kotlin学习系列(三)
    获取Android设备标识符
    Kotlin学习系列(二)
    ijkplayer实现IMediaDataSource
    Fresco添加HTTP请求头
    Kotlin学习系列(一)
    完整的Android MVP开发之旅
    Marshmallow权限使用
    APP设计与开发(ui篇)
    下载Android源码
  • 原文地址:https://www.cnblogs.com/icebutterfly/p/8920514.html
Copyright © 2011-2022 走看看