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
  • 相关阅读:
    thinkinginjava学习笔记07_多态
    thinkinginjava学习笔记06_复用类
    thinkinginjava学习笔记05_访问权限
    thinkinginjava学习笔记04_初始化与清理
    thinkinginjava学习笔记03_基本语法
    thinkinginjava学习笔记02_对象
    关于药物刺激引起的突变
    通路共出现
    关于reference-free去卷积
    一些研究生期间的反思
  • 原文地址:https://www.cnblogs.com/whatyouknow123/p/6688488.html
Copyright © 2011-2022 走看看