string to int

problem describe:

  given a string , first find the first word which is not white space;then there will be an optional '+' or '-', but the given the test set the given string will contain more than one symbol or didn't contain any number, if that you should return 0;third,you should find as many  number as possible,and change the string num into int.if the num more than 2^31-1 or smaller than -2^31, you should return 2^31- 1 or  -2^31.

 i.e.

input '+-2'

output '0'

my python solution:

class Solution(object):
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        slength = len(s)
        full, leave = divmod(slength, (numRows+numRows/2))
        left, right = divmod(leave, numRows)
        returnlist = []
        times = full
        k = 0
        if left == 0:
            left = right
            right =0
        for i in range(numRows):
            if i%2 != 0:
                for colnum in range(times):
                    returnlist[k] = s[i+(3*numRows/2)*times]
                    k += 1
                    if left != 0:
                        if i+(3*numRows/2)*(times+1)<= slength:
                            returnlist[k] = s[i+(3*numRows/2)*(times+1)]
                            k += 1
            else:
                for colnum in range(times):
                    returnlist[k] = s[i+(3*numRows/2)*times]
                    k = k+1
                    returnlist[k] = s[numRows+i/2+ (3*numRows/2)*times]
                    k += 1
                    if left !=0 and i+(3*numRows/2)*(times+1)<slength:
                        returnlist[k] = s[i+(3*numRows/2)*(times+1)]
                        k += 1
                    if right != 0 and numRows+i/2+ (3*numRows/2)*(times+1)<slength:
                        returnlist[k] = s[numRows+i/2+ (3*numRows/2)*(times+1)]
                        k += 1
        need = ''
        for each in returnlist:
            need += each
        return need

 

posted @ 2017-04-10 12:45  whatyouknow123  阅读(234)  评论(0编辑  收藏  举报