• 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
  • 相关阅读:
    关于DotNETStruts
    SQL语句导入导出大全
    一个打印Dataset的.net的打印类
    遍历指定文件夹下所有的文件
    SQL Server SQL导入导出语句
    查看OCX的属性和方法的脚本
    关于写文本文件的问题
    新钶信息系统面试
    【POJ1208】The Blocks Problem
    【POJ1363】Rails
  • 原文地址:https://www.cnblogs.com/whatyouknow123/p/6688488.html
Copyright © 2020-2023  润新知