Convert prefix to post

141 views Asked by At

I am trying to convert some C code into Go.

for i := l + 1; i < r; ++i {
    ans = max(ans, nums[l]*nums[i]*nums[r]+maxCoin(dp, l, i, nums)+maxCoin(dp, i, r, nums))
}

Go doesn't have prefix operator, how can I convert this loop to use postfix?

1

There are 1 answers

1
icza On BEST ANSWER

Use a postfix increment statement, it doesn't make any difference in this case:

for i := l + 1; i < r; i++ {
    ans = max(ans, nums[l]*nums[i]*nums[r]+maxCoin(dp, l, i, nums)+maxCoin(dp, i, r, nums))
}

The difference only matters when you're using the result of the expression formed by the increment / decrement operator, but since in Go they're not even operators but statements, it doesn't matter. For reasoning, see FAQ: Why are ++ and -- statements and not expressions? And why postfix, not prefix?