当前位置:网站首页>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;
}
}
边栏推荐
- Nacos 下载启动、配置 MySQL 数据库
- Qunhui NAS configuring iSCSI storage
- kubernetes部署loki日志系统
- C# 将网页保存为图片(利用WebBrowser)
- Network security - summary and thinking of easy-to-use fuzzy tester
- PCL calculates the intersection of three mutually nonparallel planes
- [untitled]
- 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
- C language custom type enumeration, Union (clever use of enumeration, calculation of union size)
- OpenShift 部署应用
猜你喜欢
Installing Oracle database 19C RAC on Linux
Aneng logistics' share price hit a new low: the market value evaporated by nearly 10 billion yuan, and it's useless for chairman Wang Yongjun to increase his holdings
IP protocol and IP address
C nail development: obtain all employee address books and send work notices
Minecraft plug-in service opening
小米电视不能访问电脑共享文件的解决方案
OpenFeign 簡單使用
Move a string of numbers backward in sequence
Openshift build image
What is SQL injection
随机推荐
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
[blackmail virus data recovery] suffix Rook3 blackmail virus
Linux二进制安装Oracle Database 19c
1、 QT's core class QObject
Pointer initialization
Gateway is easy to use
随笔:RGB图像颜色分离(附代码)
Tcp/ip - transport layer
zipkin 简单使用
Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel
Call Stack
D interface and domain problems
Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it
MYSQL安装出现问题(The service already exists)
IP protocol and IP address
Application of kotlin - higher order function
一、Qt的核心类QObject
HCIA—應用層
C language replaces spaces in strings with%20
Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)