zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Linux curl命令详解
    Go语言获取命令行参数
    MySQL对sum()字段 进行条件筛选,使用having,不能用where
    PHP的 first day of 和 last day of
    easyui-datagrid个人实例
    easyui-layout个人实例
    easyui-combotree个人实例
    easyui-combo个人实例
    easyui-combotree个人实例
    easyui datagrid加载数据和分页
  • 原文地址:https://www.cnblogs.com/whatyouknow123/p/6688488.html
Copyright © 2011-2022 走看看