1.只出现一次的数字
给你一个 非空 整数数组 nums ,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
你必须设计并实现线性时间复杂度的算法来解决此问题,且该算法只使用常量额外空间。
我的解法(找规律)
class Solution {
public int singleNumber(int[] nums) {
Arrays.sort(nums);
for(int i = 0; i < nums.length - 1; i += 2){
if(nums[i] != nums[i+1]){
return nums[i];
}else{
continue;
}
}
return nums[nums.length - 1];
}
}
官方答案(异或运算性质)
class Solution {
public int singleNumber(int[] nums) {
int res = 0;
for(int num : nums){
res ^= num;
}
return res;
}
}
总结,异或运算的三个性质:
- 任何数和0做异或运算,结果仍是原来的数;
- 任何数和自身做异或运算,结果为0;
- 异或运算符合交换律。
2.翻转二叉树
给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。
示例:
输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]
我的解法(无脑递归)
class Solution {
public TreeNode invertTree(TreeNode root) {
recursion(root);
return root;
}
public void recursion(TreeNode root){
if(root == null) return;
TreeNode temp = null;
temp = root.left;
root.left = root.right;
root.right = temp;
recursion(root.left);
recursion(root.right);
}
}
怎么说呢,这道题还没来的及思考就写出来了,虽然是个简单题,但是刷算法有一段时间了,这么顺利还是第一次。
3.合并二叉树
给你两棵二叉树: root1 和 root2 。
想象一下,当你将其中一棵覆盖到另一棵之上时,两棵树上的一些节点将会重叠(而另一些不会)。你需要将这两棵树合并成一棵新二叉树。合并的规则是:如果两个节点重叠,那么将这两个节点的值相加作为合并后节点的新值;否则,不为 null 的节点将直接作为新二叉树的节点。
返回合并后的二叉树。
注意: 合并过程必须从两个树的根节点开始。
输入:root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
输出:[3,4,5,5,4,null,7]
官方解法(递归)
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if(root1 == null) return root2;
if(root2 == null) return root1;
TreeNode root = new TreeNode(root1.val + root2.val);
root.left = mergeTrees(root1.left, root2.left);
root.right = mergeTrees(root1.right, root2.right);
return root;
}
}
这道题一直没有做出来,所以最后看了题解。
4.多数元素
给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
你可以假设数组是非空的,并且给定的数组总是存在多数元素。
示例 1:
输入:nums = [3,2,3]
输出:3
示例 2:
输入:nums = [2,2,1,1,1,2,2]
输出:2
我的解法(Hash)
class Solution {
public int majorityElement(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for(int num : nums){
map.put(num, map.getOrDefault(num, 0) + 1);
}
for(Map.Entry<Integer, Integer> entry : map.entrySet()){
if(entry.getValue() > (nums.length >> 1)){
return entry.getKey() ;
}
}
return -1;
}
}
官方解法(因为要找的数个数大于数组长度得一半,所以排序好后中间的数就是要找的答案)
class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length >> 1];
}
}
5.找到所有数组中消失的数字
给你一个含 n 个整数的数组 nums ,其中 nums[i] 在区间 [1, n] 内。请你找出所有在 [1, n] 范围内但没有出现在 nums 中的数字,并以数组的形式返回结果。
示例 1:
输入:nums = [4,3,2,7,8,2,3,1]
输出:[5,6]
示例 2:
输入:nums = [1,1]
输出:[2]
我的解法(暴力解法)
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> res = new ArrayList<>();
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; ++i){
map.put(nums[i], 1);
}
for(int i = 1; i <= nums.length; ++i){
if(!map.containsKey(i)) res.add(i);
}
return res;
}
}
官方解法(原地修改,本质上还是利用hash思想.,只不过空间复杂度为O(1))
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> res = new ArrayList<>();
int n = nums.length;
for(int num : nums){
int x = (num - 1) % n;
nums[x] += n;
}
for(int i = 0; i < n ; ++i){
if(nums[i] <= n) res.add(i + 1);
}
return res;
}
}