diff --git a/solution/0700-0799/0747.Largest Number At Least Twice of Others/README.md b/solution/0700-0799/0747.Largest Number At Least Twice of Others/README.md index c4a40115b42da..335c387ef59a2 100644 --- a/solution/0700-0799/0747.Largest Number At Least Twice of Others/README.md +++ b/solution/0700-0799/0747.Largest Number At Least Twice of Others/README.md @@ -58,7 +58,20 @@ ```java - +class Solution { + public int dominantIndex(int[] nums) { + int maxIndex = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] > nums[maxIndex]) + maxIndex = i; + } + for (int i = 0; i < nums.length; i++) { + if (nums[i] * 2 > nums[maxIndex] && i != maxIndex) + return -1; + } + return maxIndex; + } +} ``` ### **...** diff --git a/solution/0700-0799/0747.Largest Number At Least Twice of Others/README_EN.md b/solution/0700-0799/0747.Largest Number At Least Twice of Others/README_EN.md index b442a5b5abd08..81c1fa595dcde 100644 --- a/solution/0700-0799/0747.Largest Number At Least Twice of Others/README_EN.md +++ b/solution/0700-0799/0747.Largest Number At Least Twice of Others/README_EN.md @@ -50,7 +50,20 @@ The index of value 6 is 1, so we return 1. ### **Java** ```java - +class Solution { + public int dominantIndex(int[] nums) { + int maxIndex = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] > nums[maxIndex]) + maxIndex = i; + } + for (int i = 0; i < nums.length; i++) { + if (nums[i] * 2 > nums[maxIndex] && i != maxIndex) + return -1; + } + return maxIndex; + } +} ``` ### **...** diff --git a/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.java b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.java new file mode 100644 index 0000000000000..3561eee8bd96b --- /dev/null +++ b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.java @@ -0,0 +1,14 @@ +class Solution { + public int dominantIndex(int[] nums) { + int maxIndex = 0; + for (int i = 0; i < nums.length; i++) { + if (nums[i] > nums[maxIndex]) + maxIndex = i; + } + for (int i = 0; i < nums.length; i++) { + if (nums[i] * 2 > nums[maxIndex] && i != maxIndex) + return -1; + } + return maxIndex; + } +} \ No newline at end of file