概述
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];
}
}
最后
以上就是感动秋天为你收集整理的剑指offer专项突击版 ---- 第2天的全部内容,希望文章能够帮你解决剑指offer专项突击版 ---- 第2天所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复