当前位置:网站首页>Sword Point Offer Special Assault Edition ---- Day 2
Sword Point Offer Special Assault Edition ---- Day 2
2022-07-31 05:32:00 【Milan's little red and black】

class Solution {
public int singleNumber(int[] nums) {
int ans = 0;
for(int i = 0; i < 32; i++){
int target = 0;
for(int num : nums){
target += (num >> i) & 1;
}
ans |= (target%3) << i;
}
return ans;
}
}

class Solution{
// 暴力解法
// m 表示单词的平均长度,n 表示单词的个数
// 时间复杂度:O(n^2 * m)
// 空间复杂度:O(1)
public int maxProduct(String[] words) {
int ans = 0;
for (int i = 0; i < words.length -1; i++) {
String word1 = words[i];
for (int j = i + 1; j < words.length; j++) {
String word2 = words[j];
// 每个单词的 bitMask 会重复计算很多次
if (!hasSameChar(word1, word2)) {
ans = Math.max(ans, word1.length() * word2.length());
}
}
}
return ans;
}
// O(m^2)
private boolean hasSameChar(String word1, String word2) {
for (char c : word1.toCharArray()) {
if (word2.indexOf(c) != -1) return true;
}
return false;
}
}

class Solution {
// 时间复杂度:O(n)
// 空间复杂度:O(1)
public int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length == 0) return new int[0];
int left = 0;
int right = nums.length - 1;
while (left < right) {
int sum = nums[left] + nums[right];
if (sum == target) {
return new int[]{
left, right};
} else if (sum < target) {
left++;
} else {
right--;
}
}
return new int[0];
}
}
边栏推荐
- MySQL optimization slow log query
- Paginate the list collection and display the data on the page
- 面试官,不要再问我三次握手和四次挥手
- The interviewer asked me TCP three handshake and four wave, I really
- Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
- 剑指offer基础版 ----- 第28天
- [mysql improves query efficiency] Mysql database query is slow to solve the problem
- 基于web3.0使用钱包Metamask的三方登陆
- mysql5.7.35安装配置教程【超级详细安装教程】
- 一文了解大厂的DDD领域驱动设计
猜你喜欢

MySQL-Explain详解

Temporal介绍

About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)

pycharm专业版使用

matlab simulink欠驱动水面船舶航迹自抗扰控制研究

MySQL8.0.26安装配置教程(windows 64位)

剑指offer基础版 ---- 第27天
![2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。](/img/7f/130a9b733855a2bab07d26ffda2c49.png)
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。

Anaconda配置环境指令

STM32 - DMA
随机推荐
Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
numpy和pytorch中的元素拼接操作:stack,concatenat,cat
Redis Advanced - Cache Issues: Consistency, Penetration, Penetration, Avalanche, Pollution, etc.
MYSQL下载及安装完整教程
Anaconda配置环境指令
剑指offer基础版 ----- 第25天
Kubernetes 证书可用年限修改
【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
C语言实验二 数据类型、运算符和表达式
【一起学Rust】Rust学习前准备——注释和格式化输出
可点击也可直接复制指定内容js
再见了繁琐的Excel,掌握数据分析处理技术就靠它了
.NET-6.WinForm2.NanUI learning and summary
面试官问我TCP三次握手和四次挥手,我真的是
剑指offer基础版 --- 第21天
Swordsman Offer Special Assault Edition --- Day 3
mysql uses on duplicate key update to update data in batches
【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
C语言教程(一)-准备
C语言实验三 选择结构程序设计