当前位置:网站首页>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];
}
}
边栏推荐
- With MVC, why DDD?
- DVWA shooting range environment construction
- 剑指offer基础版--- 第23天
- 剑指offer专项突击版 ---- 第1天
- 运用flask框架发送短信验证码的流程及具体代码
- MySQL forgot password
- Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
- Centos7 install mysql5.7 steps (graphical version)
- Flink sink redis 写入Redis
- C语言的文件操作(一)
猜你喜欢

Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled. How to do this?

MYSQL下载及安装完整教程

Swordsman Offer Special Assault Edition --- Day 3

Apache DButils使用注意事项--with modifiers “public“

Lock wait timeout exceeded解决方案

MySQL transaction (transaction) (this is enough..)
![[MQ I can speak for an hour]](/img/ef/863c994ac3a7de157bd39545218558.jpg)
[MQ I can speak for an hour]

MySQL window function

C语言实验三 选择结构程序设计

目标检测学习笔记
随机推荐
Object Detection Study Notes
Flink sink redis 写入Redis
剑指offer基础版 ----第31天
Why use Flink and how to get started with Flink?
Interviewer, don't ask me to shake hands three times and wave four times again
[MQ I can speak for an hour]
MySQL(更新中)
剑指offer专项突击版 ---- 第1天
STM32 - DMA
110道 MySQL面试题及答案 (持续更新)
.NET-6.WinForm2.NanUI learning and summary
Tapdata 与 Apache Doris 完成兼容性互认证,共建新一代数据架构
Kubernetes 证书可用年限修改
matlab simulink欠驱动水面船舶航迹自抗扰控制研究
三次握手与四次挥手
C语言的文件操作(一)
pycharm专业版使用
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
datagrip带参sql查询
Mysql application cannot find my.ini file after installation