Java 中 "targat" 变量的拼写错误导致的搜索算法错误 - 解决方法
{
"title": "Java 中 "targat" 变量的拼写错误导致的搜索算法错误 - 解决方法",
"description": "本示例展示了在 Java 代码中,由于变量 "targat" 拼写错误导致搜索算法出错的问题,并提供了正确的代码修正方法。",
"keywords": "Java, 搜索算法, 变量拼写错误, 错误解决, targat, target, lowerBound, 代码修正",
"content": "The error is occurring because there is a typo in the code. The variable target is misspelled as targat in the search method. To fix the error, the code should be corrected as follows:\n\njava\nclass Solution {\n int search(List<int> nums, int target) {\n nums.sort();\n int ans = lowerBound(nums, target);\n if(ans < nums.length && nums[ans] == target) return ans;\n return -1;\n }\n int lowerBound(List<int> nums, int target) {\n int left = 0, right = nums.length;\n while (left < right) {\n int mid = left + right >> 1;\n if (nums[mid] < target) left = mid + 1;\n else right = mid;\n }\n return left;\n }\n}\n\n"}
原文地址: https://www.cveoy.top/t/topic/qgnc 著作权归作者所有。请勿转载和采集!