当前位置:网站首页>Leetcode sword finger offer brush questions - day 23
Leetcode sword finger offer brush questions - day 23
2022-07-02 08:53:00 【DEGv587】
Leetcode The finger of the sword Offer How to brush questions :
The finger of the sword Offer 39. A number that appears more than half the times in an array
Solution 1 : Sort library function + Median
class Solution {
public int majorityElement(int[] nums) {
if (nums.length == 0) return -1;
Arrays.sort(nums);
return nums[nums.length / 2];
}
}
Solution 2 :hashmap
class Solution {
public int majorityElement(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int n : nums) {
map.put(n, map.getOrDefault(n, 0) + 1);
if (map.get(n) > (nums.length / 2)) {
return n;
}
}
return -1;
}
}
Solution 3 : Moore voting , Limit for limit , Left to the end is more than half
class Solution {
public int majorityElement(int[] nums) {
int count = 0, ret = 0;
for (int n : nums) {
if (count == 0) {
ret = n;
++count;
} else {
count = ret == n ? ++count : --count;
}
}
return ret;
}
}
The finger of the sword Offer 66. Building a product array
solution : From left to right , Then multiply from right to left
class Solution {
public int[] constructArr(int[] a) {
int len = a.length;
int[] ret = new int[len];
// From left to right
int cur = 1;
for (int i = 0; i < len; ++i) {
ret[i] = cur;
cur *= a[i];
}
// From right to left
cur = 1;
for (int i = len - 1; i >= 0; --i) {
ret[i] *= cur;
cur *= a[i];
}
return ret;
}
}
边栏推荐
- Use the numbers 5, 5, 5, 1 to perform four operations. Each number should be used only once, and the operation result value is required to be 24
- kubernetes部署loki日志系统
- sqli-labs第12关
- Solid principle: explanation and examples
- Realization of basic function of sequence table
- Minecraft air Island service
- Qt的connect函数和disconnect函数
- Minecraft module service opening
- Network security - summary and thinking of easy-to-use fuzzy tester
- Kubernetes deploys Loki logging system
猜你喜欢
Linux binary installation Oracle database 19C
Linux二进制安装Oracle Database 19c
ARP及ARP欺骗
Chrome debugging
[blackmail virus data recovery] suffix Hydra blackmail virus
Minecraft group service opening
File upload and download performance test based on the locust framework
Openshift build image
Solid principle: explanation and examples
History of Web Technology
随机推荐
1、 QT's core class QObject
Minecraft插件服开服
C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
Chrome debugging
什么是SQL注入
Pointer initialization
小米电视不能访问电脑共享文件的解决方案
commands out of sync. did you run multiple statements at once
Openshift container platform community okd 4.10.0 deployment
Introduction to the basic concept of queue and typical application examples
Using recursive functions to solve the inverse problem of strings
C Gaode map obtains the address according to longitude and latitude
Openfeign facile à utiliser
Realization of basic function of sequence table
HCIA—数据链路层
将一串数字顺序后移
kubeadm部署kubernetes v1.23.5集群
Oracle 相关统计
C#钉钉开发:取得所有员工通讯录和发送工作通知