diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/README.md b/solution/1300-1399/1394.Find Lucky Integer in an Array/README.md index e2aab03d4d2ff..22408a5b44697 100644 --- a/solution/1300-1399/1394.Find Lucky Integer in an Array/README.md +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/README.md @@ -101,16 +101,17 @@ class Solution: ```java class Solution { public int findLucky(int[] arr) { - int[] cnt = new int[501]; + int[] cnt = new int[510]; for (int x : arr) { - ++cnt[x]; + cnt[x]++; } - for (int x = cnt.length - 1; x > 0; --x) { - if (x == cnt[x]) { - return x; + int ans = -1; + for (int x = 1; x < cnt.length; ++x) { + if (cnt[x] == x) { + ans = x; } } - return -1; + return ans; } } ``` diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/README_EN.md b/solution/1300-1399/1394.Find Lucky Integer in an Array/README_EN.md index cf0c8ede706fc..cd66ee2526a7b 100644 --- a/solution/1300-1399/1394.Find Lucky Integer in an Array/README_EN.md +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/README_EN.md @@ -85,16 +85,17 @@ class Solution: ```java class Solution { public int findLucky(int[] arr) { - int[] cnt = new int[501]; + int[] cnt = new int[510]; for (int x : arr) { - ++cnt[x]; + cnt[x]++; } - for (int x = cnt.length - 1; x > 0; --x) { - if (x == cnt[x]) { - return x; + int ans = -1; + for (int x = 1; x < cnt.length; ++x) { + if (cnt[x] == x) { + ans = x; } } - return -1; + return ans; } } ``` diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.java b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.java index e032656eb678a..93e6e2185b49d 100644 --- a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.java +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.java @@ -1,14 +1,15 @@ class Solution { public int findLucky(int[] arr) { - int[] cnt = new int[501]; + int[] cnt = new int[510]; for (int x : arr) { - ++cnt[x]; + cnt[x]++; } - for (int x = cnt.length - 1; x > 0; --x) { - if (x == cnt[x]) { - return x; + int ans = -1; + for (int x = 1; x < cnt.length; ++x) { + if (cnt[x] == x) { + ans = x; } } - return -1; + return ans; } } \ No newline at end of file