186.Add Strings

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u010339647/article/details/89502330

题目

Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.

Note:

The length of both num1 and num2 is < 5100.
Both num1 and num2 contains only digits 0-9.
Both num1 and num2 does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to integer directly.

链接

https://leetcode.com/problems/add-strings/

分析

主要是考虑进位的影响;

code

class Solution(object):
    
    def addStrings(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """
        """
        从后往前遍历每个字符
        """
        if len(num1) == 0:
            return num2
        if len(num2) == 0:
            return num1
        ## 永远保证num1的长度比num2长
        if len(num1) < len(num2):
            tmp = num1
            num1 = num2
            num2 = tmp
        
        flag = 0 # flag表示是否需要进位
        re = []
        i = 0
       
        while i < len(num2):
            ele = int(num1[len(num1) - 1 - i]) + int(num2[len(num2) - 1 - i]) + flag
            flag = ele/10
            ele = ele%10
            re.append(str(ele))
            i += 1
       
        while i < len(num1):
            ele = int(num1[len(num1) - 1 - i]) + flag
            flag = ele/10
            ele = ele%10
            re.append(str(ele))
            i += 1
        if flag == 1:
             re.append('1')
            
        re.reverse() 
       
        return ''.join(re)
            
展开阅读全文

Pasting Strings

07-22

DescriptionnnYou are part of a team implementing an HTML editor and have been tasked with the problem of implementing the cut/copy/paste functions. One of your goals is to preserve the formatting of selected text, even though that formatting may be determined by HTML tags outside the range of the actual selected text. nnYou will be provided with a block of formatted text, a starting position B, and an ending position E. Your program should output the text of the substring of that text from B (inclusive) to E (exclusive), prepending and appending formatting tags as necessary so that the output is well formed and has the same format as it had in its original position. nnFor the purposes of this problem, format tags consist of an opening tag (such as ``< b> "), followed by some text, followed by a closing tag (such as ``< / b> "). Opening and closing tags are paired (``< /whatever> " closes ``< whatever> ") and are considered opened between the opening tag and the closing tag. A tag may not be closed unless it is the most recent unclosed tag (e.g., ``< i> abc< b> def< / i> ghi< / b> " is illegal). A tag may not be opened if it is already open (e.g., ``< b> < b> recursive b< / b> < / b> " is illegal).nInputnnInput data will consist of multiple test cases. Each test case will consist of one line of input of the form nnnB E TEXT nnnwhere B is an integer giving the (inclusive) beginning location of the substring, E is an integer giving the (exclusive) ending location of the substring, and TEXT contains the text from which to extract the substring. The TEXT begins after a single blank character immediately following E , and continues to the end of the line. B and E will be specified so that 0 <= B <= E <= length(TEXT) . nnEnd of input will be signaled by the line ``-1 -1 " with a single space following the second -1. nnNo input line will be longer than 200 characters. nnThe TEXT will be composed of characters with an ASCII value >= 32 (the ASCII space) and <= 126 (the ASCII ' '). Opening tags will be of the form ``< X > " where X contains at least 1 character and is composed entirely of the characters `a' to `z', `A' to `Z', `0' to `9', and `-'. Closing tags will be of the form ``< /X > ". The character `< ' will only occur in the input as the first character of an opening or closing tag. nnThe input text will be well formed - all opening tags will be matched with a closing tag, all closing tags will match an opening tag, each closing tag will close the most recent unclosed tag, and tags will not be recursive (each tag must be closed prior to reopening). nn0 <= B <= E <= length(TEXT) . Neither B nor E will reference a character that is part of an opening or closing tag except for the character `< '.nOutputnnFor each test case your program should print a single line containing the substring of TEXT from B (inclusive) to E (exclusive), prepending the substring with opening tags and appending the substring with closing tags as necessary so that the output line is well formed and has the same set of open tags as when it was included in the original TEXT .nSample Inputnn0 15 Testing! n18 23 100, 1000, 10000 n4 4 123 n0 16 :-/ :-> :-) :-<-> n-1 -1nSample OutputnnTesting! n1000, n n :-/ :-> :-) :- 问答

Compare with two strings:

12-14

Compare with two strings:rnWhen I run the test program as the following using rn javac Test testStringrnrnI got the following results:rnrntest == testStringrntest1 != testStringrntest1 != testrntest2 == testrntest equal to testStringrntest1 equals to testStringrntest1 equals to testrntest2 equals to testrnrnrnSo why test1 != testString and test1 != test ?rnrnThank you!rnrnThe following are my codes:rnrnpublic static void main(String[] args) rn String test = "testString";rn String test1 = args[2];rn String test2 = test; rnrn if (test == "testString") rn System.out.println("test == testString");rn else rn System.out.println("test != testString");rn rnrn if (test1 == "testString") rn System.out.println("test1 == testString");rn else rn System.out.println("test1 != testString");rn rn rn if (test1 == test) rn System.out.println("test1 == test");rn else rn System.out.println("test1 != test");rn rnrn if (test2 == test) rn System.out.println("test2 == test");rn else rn System.out.println("test2 != test");rn rn rn if (test .equals("testString")) rn System.out.println("test equal to testString");rn else rn System.out.println("test does not equal to testString");rn rnrn if (test1 .equals("testString")) rn System.out.println("test1 equals to testString");rn else rn System.out.println("test1 does not equal to testString");rn rnrn if (test1 .equals(test)) rn System.out.println("test1 equals to test");rn else rn System.out.println("test1 does not equal to test");rn rn rn if (test2 .equals(test)) rn System.out.println("test2 equals to test");rn else rn System.out.println("test2 does not equal to test");rn rn 论坛

Checking Input Strings for Nulls

02-10

Greetings,rnrnI'm having problem with my program below. I want to check for the input of a null value.rnrnALGORITHM:rnoutput directionsrnask for inputrnif input is null (i.e. just pressing enter) then output to user null values are not allowed and ask for input again.rnif input is anything else output to user it's ok.rnrnRight now, for both scenarios, it outputs "null values are not allowed"rnWhy is this?rnAlso, how can i get it to prompt for input again after outputting that null values are not allowed?rnrnThanks in advance!rnrnrn#include rnrnusing std::cout;rnusing std::cin;rnrnint main()rnrncout << "INTRODUCTION: This program is a language recognizer emulator. \n";rn cout << "It only recognizes a language consisting of strings that have \n";rn cout << "n copies of the letter a followed by the same number of copies \n";rn cout << "of the letter b, where n > 0. For example: aaaabbbb, aabb, or ab. \n";rn cout << "\n";rn cout << "\n";rn cout << "DIRECTIONS: To test this language recognizer, please input a string \n";rn cout << "consisting of a's and b's. If the string is recognized, you will be told so. \n";rn cout << "If the string is not recognized, you will be prompted again to input \n";rn cout << "another string of a's and b's. \n";rn cout << "\n";rn cout << "\n";rn cout << "NOTE: You must enter at least one character. Null values are not allowed. \n";rn cout << "\n";rn cout << "\n";rn rnrn int intPhrase;rn char charNull = '\0';rn rn cout << "Please enter a string of characters consisting of a's and b's. \n";rn cin >> intPhrase;rnrnrn//...CHECK FOR NULL VALUE...rn rn if (intPhrase == charNull) rn cout << "Null values are not allowed. \n";rn cout << "Please enter a string of characters consisting of a's and b's. \n";rn cin >> intPhrase;rn rn else rn cout << "At least it's not a null!. \n";rn rn rnrn int x;rn cin >> x;rn return 0;rnrn 论坛

没有更多推荐了,返回首页