![]() ![]() So instead of making myPow a method of some class, you should consider just making it a function. In another language, we might use a record or a struct for these method-less values.) (Classes which only provide state with no, or minimal, behaviour are probably more common. If a class has no state (like your Solution50 class), or no methods, then chances are very good that the class isn’t pulling its weight and should be removed. The general guideline is that every class should have both state (data) and behaviour (one or more methods). The classes often don’t do anything except hold that method. In Java, every function has to be put into a method of some class. It reads like you are trying to program Java in Python. Its probably just a case of somebody thought it was a good idea at the time.īy the way, each of the builtin exponent operator **, the pow() builtin function, and math.pow function round 2.0 to the power of (negative huge number) to 0.0.īy the way, you have two classes which do nothing and exist for no reason other than to hold a function. There’s probably no real logic behind the difference. Floating point multiplication in Python must follow the rules set up by the IEEE-754 standard, which has multiplication overflow to the special value float(‘inf’).īut for reasons now lost, whoever designed Python’s exponent operator, and the pow() function, made a different decision to raise OverflowError instead. That is probably buried deep, deep in the mists of history, when Python was first written. Why the difference between multiplication and exponentiation? Which rounds to zero, so you get the final result of 0.0. For brevity I won’t write them all down in full: While the second computes the similar sequence, but using multiplication. ![]() Your first method tries to compute the sequence of powers: ![]() Mainly there as an example, use standard containers.The two methods behave differently because they are written differently, one uses the exponentiation operator ** and the other just uses multiplication. ![]() Also generally this is only useful to people who already know about it and know its limitations well. Note about alloca: As someone pointed out errors can be caused by placing arrays too large on the stack, so be careful. If you want dynamic arrays use new or you can use some C style alloca to still be standard compliant, but generally stl containers are your friend so you could also just use a std::vector>, like the problem does. One example on how it can be rewritten is: for(int a = -1 a = board.size() ||Īn other note I mentioned in the comment int mat is not standard C++. I recommend you clean up the bounds checking and neighbor counting so that it is not only more compact but much easier to look through as well. I'm getting a runtime error while submitting the code on Leetcode.įor (row = board.begin() row != board.end() row++) then i = 0 & j = 0 is true, but mat is out of bounds, possibly filled with garbage which then gets dereferenced. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |