[問題] Leetcode 744

看板C_and_CPP (C/C++)作者時間4年前 (2021/07/09 14:40), 4年前編輯推噓4(402)
留言6則, 5人參與, 4年前最新討論串1/3 (看更多)
https://i.imgur.com/gsNoiha.png
https://i.imgur.com/ZRff8qC.png
Leetcode 744. Find Smallest Letter Greater Than Target 一樣的code在 leetcode 上執行和在 VScode 執行上結果不同 自己trace過一遍覺得應該沒問題 是有 bug 嗎? 還是.....? vector<char> letters = {'c' ,'f', 'j'}; char ans; char target = 'k'; int left = 0; int right = letters.size()-1; while(left<=right){ int m = left+(right-left)/2; if(letters[m]>target){ ans = letters[m]; right = m-1; } else if(letters[m]<=target) left = m+1; } if(ans>target) cout<<ans<<endl; else cout<<letters[0]<<endl; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.10.21.68 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1625812804.A.981.html ※ 編輯: Kuba4ma (101.10.21.68 臺灣), 07/09/2021 14:55:12

07/09 14:58, 4年前 , 1F
ans的值有可能沒更新
07/09 14:58, 1F

07/09 15:09, 4年前 , 2F
謝謝一樓 ans確實沒更新 但不知道為什麼vscode會輸出c
07/09 15:09, 2F

07/09 15:10, 4年前 , 3F
改成 char ans = 'a' 就可以過 leetcode了
07/09 15:10, 3F

07/09 17:45, 4年前 , 4F
lower_bound 好用
07/09 17:45, 4F

07/09 18:34, 4年前 , 5F
自從幫檸檬羊解兩次bug都不理人我就懶得幫人解了
07/09 18:34, 5F

07/10 13:46, 4年前 , 6F
變數沒有預設值就去調用也算是一種coding錯誤
07/10 13:46, 6F
文章代碼(AID): #1Wv-z4c1 (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1Wv-z4c1 (C_and_CPP)