当前位置:网站首页>剑指offer专项突击版 ---- 第2天
剑指offer专项突击版 ---- 第2天
2022-07-31 05:09:00 【米兰的小红黑】

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];
}
}
边栏推荐
- Go中间件
- Linux系统安装mysql(rpm方式安装)
- sql语句之多表查询
- SQL行列转换
- .NET-9. A mess of theoretical notes (concepts, ideas)
- 2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
- Redis的初识
- 可点击也可直接复制指定内容js
- A complete introduction to JSqlParse of Sql parsing and conversion
- Minio上传文件ssl证书不受信任
猜你喜欢

STM32——DMA

Distributed transaction processing solution big PK!

TOGAF之架构标准规范(一)

再见了繁琐的Excel,掌握数据分析处理技术就靠它了

分布式事务处理方案大 PK!

Mysql——字符串函数

Go language study notes - dealing with timeout problems - Context usage | Go language from scratch

关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)

Unity resources management series: Unity framework how to resource management

Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
随机推荐
Redis的初识
Tapdata 与 Apache Doris 完成兼容性互认证,共建新一代数据架构
Workflow番外篇
太厉害了,终于有人能把文件上传漏洞讲的明明白白了
mysql 的简单运用命令
【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
Mysql——字符串函数
如何将项目部署到服务器上(全套教程)
mysql uses on duplicate key update to update data in batches
Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
[MQ I can speak for an hour]
Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
.NET-6.WinForm2.NanUI learning and summary
快速掌握并发编程 --- 基础篇
[Cloud Native] DevOps (5): Integrating Harbor
Shell重油常压塔模拟仿真与控制
tf.keras.utils.get_file()
CentOS7 - yum install mysql
Redis Advanced - Cache Issues: Consistency, Penetration, Penetration, Avalanche, Pollution, etc.
Mysql应用安装后找不到my.ini文件