当前位置:网站首页>剑指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];
}
}
边栏推荐
猜你喜欢

Temporal介绍

Mysql应用安装后找不到my.ini文件

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

工作流编排引擎-Temporal
![[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade](/img/af/7a2cdcc6535c04c508c9ddf9ee0cb2.png)
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade

Multiple table query of sql statement

MySQL优化之慢日志查询

面试官,不要再问我三次握手和四次挥手

MySQL-如何分库分表?一看就懂

Simple read operation of EasyExcel
随机推荐
Numpy中np.meshgrid的简单用法示例
有了MVC,为什么还要DDD?
一文了解大厂的DDD领域驱动设计
CentOS7 install MySQL graphic detailed tutorial
【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
MySQL transaction (transaction) (this is enough..)
SQL语句中对时间字段进行区间查询
mysql stored procedure
With MVC, why DDD?
a different object with the same identifier value was already associated with the session
Multiple table query of sql statement
Lock wait timeout exceeded解决方案
C语言如何分辨大小端
datagrip带参sql查询
质量小议12 -- 以测代评
.NET-9. A mess of theoretical notes (concepts, ideas)
mysql uses on duplicate key update to update data in batches
Linux系统安装mysql(rpm方式安装)
Redis Advanced - Cache Issues: Consistency, Penetration, Penetration, Avalanche, Pollution, etc.
Typec手机有线网卡网线转网口转接口快充方案