Why use ord?? Since we know it can either be 0 or 1. Why not just a string comparison and assign. Like, if it's "0", then digitA is 0, else 1 Same for digitB Good implementation though
@@surbhirohilla5139 ord() converts each string value to the integer representation of the unicode value (i.e. ASCII table), so subtracting ord("0") from ord("n") where 'n' is any number 0-9 will return that number. In ASCII, "0"=48 and "9"=57, so in that case `ord("9") - ord("0")` = 57 - 48 = 9
Isn't the time complexity quadratic since the addition of the 'char' string to the 'res' string copies all the contents and creates a new string for every loop since strings in python are immutable?
Yes. I'd append to the result string. And then reverse the string once in the end. Or push the result values to a stack and then form a result string in the end popping out from the stack.
code: class Solution: def addStrings(self, a: str, b: str) -> str: ans="" carry=0 a,b=a[::-1],b[::-1] for i in range(max(len(a),len(b))): digitA = ord(a[i])-ord('0') if i
@@triplestrikee875 not the same.. Prepending char in the beginning of the string makes python have to copy all the result values everytime. It's better to append chars throughout and then reverse before returning in the end
Convert the array into non non-decreasing array by replacing elements with its subarray sum if necessary. for example, input=[3, 8, 5, 2, 10], output=[3,8,17] input=[1,2,3,4,5],output=[1,2,3,4,5] input=[5,4,3,2,1],output=[5,10] input=[],output=[] Please solve this problem.
one thing I never knew before is that you can use f-strings to convert int to binary. def binary_conversion(num): return f'{num:b}' so binary_conversion(1000) will get a result of 1111101000
thank u bro it is done and passed but I don't understand what I'm doing so what should I do to be better in Leetcode for everything I must find a tutorial so the tutorial killing me bro please help me out so more than a year I did a lot of HTML CSS javascript and 1000 of course and now python, why I can't, make a project and why I can't solve one easy problem in Leetcode or code war so I don't know bro and I fill I know everything but I can't code by my self must follow someone bro thx
yeah same feeling. I'm doing the Blind 75 Easy questions. Out of 20 questions I have attempted, I don't know how solve any :) It is what it is. I'm not smart, but that's ok. Traditional hardwork and repetition has never failed me. Good luck to you bro, for whatever reasons you're studying DSA.
Why use ord?? Since we know it can either be 0 or 1. Why not just a string comparison and assign. Like, if it's "0", then digitA is 0, else 1 Same for digitB Good implementation though
i have a question for that carry = total//2 what if we add only 1 and 0 this still will give me a carry even though the result will be just 1 can anyone please help me with this?
that's what I did. I guess technically by padding you're taking up more space and the operation isn't free since you're creating a new string and iterating over the existing one.
Hello I hava a doubt can we use inbuild function like bin class Solution: def addBinary(self, a: str, b: str) -> str: return bin(int(a, 2) + int(b, 2))[2:] the code works pretty efficiently but is it the right way?