为什么当'++'运算符出现后,我的代码为什么会与i ++和numsWon ++混淆?

问题描述

我在LeetCode上遇到问题1535(找到阵列游戏的获胜者)时遇到了麻烦。我决定使用看起来像这样的最佳解决方案:

def readimg(image,write=False):
    import pytesseract
    from PIL import Image

    # opening an image from the source path
    if isinstance(image,str):
        img = Image.open(image)
        img = img.convert('RGBA')
    else:
        img = image
    img = img.convert('RGB')  # Worse results if not reconverted??

    img.show()
    # path where the tesseract module is installed
    pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe'

    # converts the image to result and saves it into result variable
    result = pytesseract.image_to_string(img)

    # write text in a text file and save it to source path
    if "" in result:  # Catch some Garbage text
        result = result[:-2]
    result = result.strip()  # Clean newlines

    if write:
        with open('output.txt',mode='a') as file:
            file.write(result)

    print(result)
    return result


def improve_img(image):
    from PIL import Image,ImageOps

    if isinstance(image,str):  # if link,open it
        im = Image.open(image)
    else:
        im = image
    im = im.convert('RGBA')

    thresh = 254  # https://stackoverflow.com/questions/9506841/using-python-pil-to-turn-a-rgb-image-into-a-pure-black-and-white-image
    fn = lambda x: 255 if x > thresh else 0
    r = im.convert('L').point(fn,mode='1')

    #r = im.convert('RGB')
    #r = ImageOps.invert(r)
    #r = im.convert('L')
    #r.save("test.png")
    #r.show()

    return r


if __name__ == '__main__':
    test = improve_img('img/testtext1.png')
    readimg(test)

但是,我是这样实现的:

int getWinner(vector<int>& A,int k) {
    int cur = A[0],win = 0;
    for (int i = 1; i < A.size(); ++i) {
        if (A[i] > cur) {
            cur = A[i];
            win = 0;
        }
        if (++win == k) break;
    }
    return cur;
}

直到我切换了class Solution { public: int getWinner(vector<int>& arr,int k) { int win = arr[0]; int numsWon = 0; for(int i = 1; i < arr.size(); i++) { if(arr[i] > win) { numsWon = 0; win = arr[i]; } if(numsWon++ == k) break; } return win; } }; 运算符,该代码才起作用。有人知道为什么吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)