首页 > 其他分享 >LeetCode 1287. Element Appearing More Than 25% In Sorted Array

LeetCode 1287. Element Appearing More Than 25% In Sorted Array

时间:2024-05-13 09:07:59浏览次数:27  
标签:1287 Appearing int arr 25% return length integer LeetCode

原题链接在这里:https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/description/

题目:

Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.

Example 1:

Input: arr = [1,2,2,6,6,6,6,7,10]
Output: 6

Example 2:

Input: arr = [1,1]
Output: 1

Constraints:

  • 1 <= arr.length <= 104
  • 0 <= arr[i] <= 105

题解:

To find the answer, for the interval length t = arr.length / 4, the beginning arr[i] == arr[i + t], then that is the answer.

Time Complexity: O(n). n = arr.length.

Space: O(1).

AC Java:

 1 class Solution {
 2     public int findSpecialInteger(int[] arr) {
 3         int n = arr.length;
 4         int t = n / 4;
 5         for(int i = 0; i < n - t; i++){
 6             if(arr[i] == arr[i + t]){
 7                 return arr[i];
 8             }
 9         }
10 
11         return -1;
12     }
13 }

 

标签:1287,Appearing,int,arr,25%,return,length,integer,LeetCode
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18188589

相关文章

  • 【LeetCode 410】分割数组的最大值
    题目描述原题链接:LeetCode.410分割数组的最大值解题思路分割的是连续子数组,所以模拟分割求子数组之和可以利用前缀和计算;设前缀和数组preSume[i]含义为[0,..,i]范围元素之和,某个子数组subArray[i,j]的元素和就是preSum[j]-preSum[i];求K个子数组元素和的最大值能转换......
  • 【LeetCode 162】寻找峰值
    题目描述原题链接:LeetCode.162寻找峰值解题思路数组相邻元素不相等,峰值可能有多个,整个数组的最大值肯定是峰值之一,直接遍历数组获取最大值也能得到答案;但是写明复杂度要求O(logn)就是否定了最简单的O(n)遍历解法,需要用二分法;按照题意数组边界另一端等同于无穷小,可......
  • 669. 修剪二叉搜索树(leetcode)
    https://leetcode.cn/problems/trim-a-binary-search-tree/description/要点是区分在区间左边还是右边,在区间左边那么右子树也还有必要去查找删除,右边同理,返回的是删除后新树的根节点要注意函数要实现单层逻辑和完成闭环语义classSolution{//查找要删除的节点,进行......
  • 450. 删除二叉搜索树中的节点(leetcode)
    https://leetcode.cn/problems/delete-node-in-a-bst/要点是确定函数语义,单层递归逻辑,确定好有返回值的这种写法,分析出5种情况,递归的时候接收好递归的返回的新树的根节点即可classSolution{//函数语义(单层递归逻辑):查找要删除的节点,并且返回删除节点后新的树的......
  • [LeetCode] 最短的桥 双BFS Java
    Problem:934.最短的桥目录思路复杂度Code思路先找到第一个岛屿,根据每一个岛屿的岛屿块的位置多源查找这个块与第二个岛屿的距离,先找到的就是最少的距离同时,将已遍历过的岛屿标记为-1,避免重复入队复杂度时间复杂度:添加时间复杂度,示例:$O(n^2)$空间复杂度:添......
  • LeetCode 1186. Maximum Subarray Sum with One Deletion
    原题链接在这里:https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/description/题目:Givenanarrayofintegers,returnthemaximumsumfora non-empty subarray(contiguouselements)withatmostoneelementdeletion. Inotherwords,youwa......
  • LeetCode 2210. Count Hills and Valleys in an Array
    原题链接在这里:https://leetcode.com/problems/count-hills-and-valleys-in-an-array/description/题目:Youaregivena 0-indexed integerarray nums.Anindex i ispartofa hill in nums iftheclosestnon-equalneighborsof i aresmallerthan nums[i].......
  • 235. 二叉搜索树的最近公共祖先(leetcode)
    https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/要点是如果root是在p和q之间的值,意味着已经找到了最近公共祖先/***Definitionforabinarytreenode.*structTreeNode{*intval;*TreeNode*left;*TreeNode*rig......
  • 236. 二叉树的最近公共祖先(leetcode)
    https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/description/要点是后序遍历,这样就可以从树底开始搜索,寻找最近公共祖先classSolution{public://返回的是最近公共祖先,若返回null则说明没有TreeNode*lowestCommonAncestor(TreeNode*r......
  • leetCode 001.两数之和
    给定一个整数数组nums和一个整数目标值target,请你在该数组中找出和为目标值target的那两个整数,并返回它们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。你可以按任意顺序返回答案。示例1:输入:nums=[2,7,11,15],ta......