当前位置:网站首页>剑指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];
}
}
边栏推荐
- 【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级
- 1. Get data - requests.get()
- CentOS7 安装MySQL 图文详细教程
- tf.keras.utils.get_file()
- Anaconda配置环境指令
- centos7安装mysql5.7
- MySQL8.0.26安装配置教程(windows 64位)
- Minio upload file ssl certificate is not trusted
- centos7安装mysql5.7步骤(图解版)
- Unity mobile game performance optimization series: performance tuning for the CPU side
猜你喜欢

CentOS7 安装MySQL 图文详细教程

MySQL transaction isolation level, rounding

mysql5.7.35安装配置教程【超级详细安装教程】

一文了解大厂的DDD领域驱动设计

Distributed transaction processing solution big PK!

Anaconda配置环境指令

Unity resources management series: Unity framework how to resource management

docker安装postgresSQL和设置自定义数据目录

矩池云快速安装torch-sparse、torch-geometric等包

12个MySQL慢查询的原因分析
随机推荐
MySQL事务(transaction) (有这篇就足够了..)
DVWA靶场环境搭建
Mysql——字符串函数
DVWA安装教程(懂你的不懂·详细)
matlab simulink欠驱动水面船舶航迹自抗扰控制研究
再见了繁琐的Excel,掌握数据分析处理技术就靠它了
账号或密码多次输入错误,进行账号封禁
MySQL (updating)
Sql解析转换之JSqlParse完整介绍
CentOS7 —— yum安装mysql
centos7安装mysql5.7
MySQL-如何分库分表?一看就懂
Tapdata 与 Apache Doris 完成兼容性互认证,共建新一代数据架构
DVWA之SQL注入
Centos7 install mysql5.7
面试官,不要再问我三次握手和四次挥手
Go中间件
Numpy中np.meshgrid的简单用法示例
Flink sink redis 写入Redis
一文了解大厂的DDD领域驱动设计