• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
数仙据灵
博客园    首页    新随笔    联系   管理    订阅  订阅

python 的for与while 的i改变

最近使用实验楼撸代码 http://www.shiyanlou.com/register?inviter=NTY0MzE5NDE2NjM5

做一道count and say 的算法题的时候,有c++语法的解题答案,我改成用python

题目:

The count-and-say sequence is the sequence of integers beginning as follows:

1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11.

11 is read off as "two 1s" or 21.

21 is read off as "one 2, then one 1" or 1211.

Given an integer n, generate the nth sequence.

Example
Given n = 5, return "111221".

Note
The sequence of integers will be represented as a string.

 在用python写的时候 我用了for中有while循环,想在while中改变i的值,也能影响外层的for中的i,但是我发现改变里层的i与外层for的i没有影响,导致程序出错

这是c++解法:

string countAndSay(int n) {
    if (n == 0) return "";
    string res = "1";
    while (--n) {
        string cur = "";
        for (int i = 0; i < res.size(); i++) {
            int count = 1;
             while ((i + 1 < res.size()) && (res[i] == res[i + 1])){
                count++;   
                i++;
            }
            cur += to_string(count) + res[i];
        }
        res = cur;
    }
    return res;
}

python也使用for和while时,内层的while修改了i值并不会传递到外层, 使重复数据被加入结果集

class Solution:
        def countAndSay(self,n):
                if n==0 :
                        return ""
                res="1"
                n=n-1
                while n:
                        print ('n:',n)
                        print('res length',len(res))
                        cur=""
                        i=0
                        for  i in range(len(res)):
                                count=1
                                print 'i in for:',i
                                while((i+1<len(res)) and (res[i]==res[i+1])):
                                        print "i in while,before +1",i,'count',count
                                        count=count+1
                                        i=i+1
                                        print 'i in while:',i,'count:',count
                                cur =cur+ str(count)+res[i]
                                print ('cur:',cur)

                        res=cur
                        print 'res:',res
                        n=n-1
                return res
aa=Solution()
print (aa.countAndSay(4))

 结果是:

shiyanlou:Code/ $ python countAndSay.py                                                                [12:35:02]
('n:', 3)
('res length', 1)
i in for: 0
('cur:', '11')
res: 11
('n:', 2)
('res length', 2)
i in for: 0
i in while,before +1 0 count 1
i in while: 1 count: 2
('cur:', '21')
i in for: 1
('cur:', '2111')
res: 2111
('n:', 1)
('res length', 4)
i in for: 0
('cur:', '12')
i in for: 1
i in while,before +1 1 count 1
i in while: 2 count: 2
i in while,before +1 2 count 2
i in while: 3 count: 3
('cur:', '1231')
i in for: 2
i in while,before +1 2 count 1
i in while: 3 count: 2
('cur:', '123121')
i in for: 3
('cur:', '12312111')
res: 12312111
12312111

 

 

python 改成while嵌套while时,程序就没有问题了,因为内层修改的i会传递到上一层

class Solution:
        def countAndSay(self,n):
                if n==0 :
                        return ""
                res="1"
                n=n-1
                while n:
                        print ('n:',n)
                        print('res length',len(res))
                        cur=""
                        i=0
                        while i < len(res):
                                count=1
                                print 'i in for:',i
                                while((i+1<len(res)) and (res[i]==res[i+1])):
                                        print "i in while,before +1",i,'count',count
                                        count=count+1
                                        i=i+1
                                        print 'i in while:',i,'count:',count
                                cur =cur+ str(count)+res[i]
                                print ('cur:',cur)
                                i=i+1
                        res=cur
                        print 'res:',res
                        n=n-1
                return res
aa=Solution()
print (aa.countAndSay(4))

 结果是:

shiyanlou:Code/ $ python countAndSay.py                                                                [11:13:53]
('n:', 3)
('res length', 1)
i in for: 0
('cur:', '11')
res: 11
('n:', 2)
('res length', 2)
i in for: 0
i in while,before +1 0 count 1
i in while: 1 count: 2
('cur:', '21')
res: 21
('n:', 1)
('res length', 2)
i in for: 0
('cur:', '12')
i in for: 1
('cur:', '1211')
res: 1211
1211

 

posted on 2017-04-06 12:36  数仙据灵  阅读(1817)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3