当前位置:网站首页>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];
}
}
边栏推荐
- Reference code series_1. Hello World in various languages
- 剑指offer专项突击版 ---- 第 6 天
- mysql stored procedure
- 【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
- Mysql——字符串函数
- 质量小议12 -- 以测代评
- 可点击也可直接复制指定内容js
- The monitoring of Doris study notes
- 【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
- Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
猜你喜欢
MySQL forgot password
关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
限流的原理
MySQL8--Windows下使用压缩包安装的方法
With MVC, why DDD?
110道 MySQL面试题及答案 (持续更新)
12个MySQL慢查询的原因分析
信息系统项目管理师核心考点(五十五)配置管理员(CMO)的工作
MySQL (updating)
Interviewer, don't ask me to shake hands three times and wave four times again
随机推荐
面试官问我TCP三次握手和四次挥手,我真的是
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍
Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
Simple read operation of EasyExcel
MYSQL一站式学习,看完即学完
SQL statement to range query time field
MySQL(更新中)
CentOS7 - yum install mysql
tf.keras.utils.pad_sequences()
面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
剑指offer基础版 ----- 第25天
Kubernetes 证书可用年限修改
MySQL8--Windows下使用压缩包安装的方法
Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
限流的原理
Shell重油常压塔模拟仿真与控制
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade
CentOS7 —— yum安装mysql
Typec手机有线网卡网线转网口转接口快充方案
MySQL-Explain详解