当前位置:网站首页>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];
}
}
边栏推荐
- Simple read operation of EasyExcel
- Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work
- Duplicate entry 'XXX' for key 'XXX.PRIMARY' solution.
- [mysql improves query efficiency] Mysql database query is slow to solve the problem
- Pytorch教程Introduction中的神经网络实现示例
- Typec手机有线网卡网线转网口转接口快充方案
- 剑指offer基础版 ----- 第28天
- C语言教程(三)-if和循环
- 一文了解大厂的DDD领域驱动设计
- 面试官,不要再问我三次握手和四次挥手
猜你喜欢
【一起学Rust】Rust学习前准备——注释和格式化输出

MySQL(更新中)

面试官问我TCP三次握手和四次挥手,我真的是

Centos7 install mysql5.7 steps (graphical version)

剑指offer基础版 --- 第24天
![<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍](/img/a4/8c75fab6a9858c5ddec25f6a8300fb.png)
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍

matlab abel变换图片处理

110 MySQL interview questions and answers (continuously updated)

Simple read operation of EasyExcel

限流的原理
随机推荐
数据库上机实验5 数据库安全性
面试官问我TCP三次握手和四次挥手,我真的是
剑指offer基础版 ----- 第28天
数据库上机实验6 数据库完整性
剑指offer专项突击版 ---- 第1天
wx.miniProgram.navigateTo在web-view中跳回小程序并传参
剑指offer基础版 ---- 第27天
torch.normal函数用法
mysql 的简单运用命令
[MQ I can speak for an hour]
TOGAF之架构标准规范(一)
Apache DButils使用注意事项--with modifiers “public“
Temporal对比Cadence
MySQL8.0.26安装配置教程(windows 64位)
Unity Framework Design Series: How Unity Designs Network Frameworks
关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
分布式事务——分布式事务简介、分布式事务框架 Seata(AT模式、Tcc模式、Tcc Vs AT)、分布式事务—MQ
C语言实验二 数据类型、运算符和表达式
基于flask的三方登陆的流程
剑指offer基础版 ---- 第26天