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

 

转载于:https://www.cnblogs.com/songjiaying/p/6672876.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值