133 lines
5.0 KiB
Java
133 lines
5.0 KiB
Java
package com.iqiaoxu.code;
|
|
|
|
import javafx.util.Pair;
|
|
|
|
import java.util.*;
|
|
|
|
public class Solution {
|
|
public int xorOperation(int n, int start) {
|
|
return (start & 3) < 2 ? ((n & 1) == 0) ? (n & 3) : (start + 2 * n - 3 + (n & 3)) : ((n & 1) == 0) ? ((start + (n - 1) * 2) ^ (start - 2 + (n & 3))) : (start + 1 - (n & 3));
|
|
}
|
|
|
|
public int mySqrt(int x) {
|
|
double xHalf = 0.5 * x;
|
|
long i = Double.doubleToLongBits(x);
|
|
i = 0x1FF7A3BEA91D9B1BL + (i >> 1);
|
|
double f = Double.longBitsToDouble(i);
|
|
f = f * 0.5 + xHalf / f;
|
|
f = f * 0.5 + xHalf / f;
|
|
f = f * 0.5 + xHalf / f;
|
|
return (int) f;
|
|
}
|
|
|
|
public int deleteAndEarn(int[] nums) {
|
|
List<Integer> sum = new ArrayList<>(Collections.nCopies(Arrays.stream(nums).max().orElse(0) + 1, 0));
|
|
Arrays.stream(nums).forEach(v -> sum.set(v, sum.get(v) + v));
|
|
return sum.stream().reduce(new Pair<>(0, 0), ((acc, x) -> new Pair<>(acc.getValue(), Integer.max(acc.getKey() + x, acc.getValue()))), (a, b) -> new Pair<>(0, 0)).getValue();
|
|
}
|
|
|
|
public int rob(int[] nums) {
|
|
return Arrays.stream(nums).boxed().reduce(new Pair<>(0, 0), ((a, x) -> new Pair<>(a.getValue(), Integer.max(a.getKey() + x, a.getValue()))), (a, b) -> new Pair<>(0, 0)).getValue();
|
|
}
|
|
|
|
public int rob2(int[] nums) {
|
|
return Integer.max(Integer.max(Arrays.stream(nums).boxed().limit(nums.length - 1).reduce(new Pair<>(0, 0), ((a, x) -> new Pair<>(a.getValue(), Integer.max(a.getKey() + x, a.getValue()))), (a, b) -> new Pair<>(0, 0)).getValue(), Arrays.stream(nums).boxed().skip(1).reduce(new Pair<>(0, 0), ((a, x) -> new Pair<>(a.getValue(), Integer.max(a.getKey() + x, a.getValue()))), (a, b) -> new Pair<>(0, 0)).getValue()), nums[0]);
|
|
}
|
|
|
|
/**
|
|
* 268.丢失的数字
|
|
*
|
|
* @param nums 数组
|
|
* @return 丢失的数字
|
|
* @see <a href="https://leetcode-cn.com/problems/missing-number/">原题链接</a>
|
|
*/
|
|
public int missingNumber(int[] nums) {
|
|
return new int[]{nums.length, 1, nums.length + 1, 0}[nums.length & 3] ^ Arrays.stream(nums).reduce(0, (o, n) -> o ^ n);
|
|
}
|
|
|
|
private boolean check(TreeNode l, TreeNode r) {
|
|
return (l == null && r == null) || l != null && r != null && l.val == r.val && check(l.left, r.right) && check(l.right, r.left);
|
|
}
|
|
|
|
/**
|
|
* 101.对称二叉树
|
|
*
|
|
* @param root 二叉树
|
|
* @return 是否是对称二叉树
|
|
* @see <a href="https://leetcode-cn.com/problems/symmetric-tree/">原题链接</a>
|
|
*/
|
|
public boolean isSymmetric(TreeNode root) {
|
|
/*if (root != null) {
|
|
Queue<TreeNode> q = new LinkedList<>();
|
|
q.offer(root.left);
|
|
q.offer(root.right);
|
|
while (!q.isEmpty()) {
|
|
TreeNode l = q.poll();
|
|
TreeNode r = q.poll();
|
|
if (l == null && r == null) {
|
|
continue;
|
|
}
|
|
if (l == null || r == null || l.val != r.val) {
|
|
return false;
|
|
}
|
|
q.offer(l.left);
|
|
q.offer(r.right);
|
|
q.offer(l.right);
|
|
q.offer(r.left);
|
|
}
|
|
}
|
|
return true;*/
|
|
/*return root == null || check(root.left, root.right);*/
|
|
return root == null ||
|
|
(root.left == null && root.right == null) ||
|
|
!(root.left == null || root.right == null || root.left.val != root.right.val) &&
|
|
isSymmetric(new TreeNode(0, root.left.left, root.right.right)) &&
|
|
isSymmetric(new TreeNode(0, root.left.right, root.right.left));
|
|
}
|
|
|
|
/**
|
|
* 640.求解方程
|
|
* @param equation 方程式
|
|
* @return 解
|
|
* @see <a href="https://leetcode-cn.com/problems/solve-the-equation/">原题链接</a>
|
|
*/
|
|
public String solveEquation(String equation) {
|
|
int a = 0, b = 0, c = 0, sign = 1, left = 1;
|
|
boolean hasC = false;
|
|
for (byte z : equation.getBytes()) {
|
|
switch (z) {
|
|
case 'x':
|
|
a += hasC ? sign * left * c : sign * left;
|
|
c = 0;
|
|
hasC = false;
|
|
break;
|
|
case '+':
|
|
b += sign * left * c;
|
|
c = 0;
|
|
sign = 1;
|
|
hasC = false;
|
|
break;
|
|
case '-':
|
|
b += sign * left * c;
|
|
c = 0;
|
|
sign = -1;
|
|
hasC = false;
|
|
break;
|
|
case '=':
|
|
b += sign * left * c;
|
|
c = 0;
|
|
sign = 1;
|
|
left = -1;
|
|
hasC = false;
|
|
break;
|
|
default:
|
|
c = c * 10 + z - '0';
|
|
hasC = true;
|
|
break;
|
|
}
|
|
}
|
|
b += sign * left * c;
|
|
return a == 0 ? b == 0 ? "Infinite solutions" : "No solution" : "x=" + -b / a;
|
|
}
|
|
}
|