当前位置:网站首页>剑指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];
}
}
边栏推荐
- MySQL optimization slow log query
- Temporal对比Cadence
- MySQL-Explain详解
- ES source code API call link source code analysis
- Centos7 install mysql5.7
- MySQL database installation (detailed)
- 如何将项目部署到服务器上(全套教程)
- DVWA installation tutorial (understand what you don't understand · in detail)
- tf.keras.utils.pad_sequences()
- MySQL开窗函数
猜你喜欢
Minesweeper game (written in c language)
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
MySQL-如何分库分表?一看就懂
[mysql improves query efficiency] Mysql database query is slow to solve the problem
工作流编排引擎-Temporal
Lock wait timeout exceeded解决方案
DVWA安装教程(懂你的不懂·详细)
With MVC, why DDD?
基于web3.0使用钱包Metamask的三方登陆
分布式事务——分布式事务简介、分布式事务框架 Seata(AT模式、Tcc模式、Tcc Vs AT)、分布式事务—MQ
随机推荐
MYSQL下载及安装完整教程
ABC D - Distinct Trio (Number of k-tuples
Flink sink redis 写入Redis
CentOS7 —— yum安装mysql
Paginate the list collection and display the data on the page
Duplicate entry 'XXX' for key 'XXX.PRIMARY' solution.
Interviewer, don't ask me to shake hands three times and wave four times again
mysql使用on duplicate key update批量更新数据
限流的原理
mysql uses on duplicate key update to update data in batches
mysql存储过程
SQL statement to range query time field
Redis进阶 - 缓存问题:一致性、穿击、穿透、雪崩、污染等.
MySQL优化之慢日志查询
【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
DVWA installation tutorial (understand what you don't understand · in detail)
Linux系统安装mysql(rpm方式安装)
Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
pycharm专业版使用