修改gitignore、213.打家劫舍II的java代码、添加js

This commit is contained in:
游由 2021-07-14 10:25:33 +08:00
parent d401a3d9ef
commit 3d9454e25e
3 changed files with 47 additions and 12 deletions

19
.gitignore vendored
View File

@ -1,19 +1,16 @@
# Created by .ignore support plugin (hsz.mobi)
### Rust template
# Generated by Cargo
# will have compiled files and executables
**/.idea
# rust
/rust/target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
rust/Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
**/.idea
java/java.iml
# java
java/*.iml
java/out/
# cpp
cpp/cmake-build-debug/
# python
**/venv/

27
JavaScript/my_leetcode.js Normal file
View File

@ -0,0 +1,27 @@
/**
* 1486.数组异或操作
* @param {number} n
* @param {number} start
* @return {number}
* @see https://leetcode-cn.com/problems/xor-operation-in-an-array/
*/
var xorOperation = function (n, 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));
};
/**
* 69.x 的平方根
* @param {number} x
* @return {number}
* @see https://leetcode-cn.com/problems/sqrtx/
*/
var mySqrt = function (x) {
const xHalf = 0.5 * x;
let i = new BigInt64Array(new Float64Array([x]).buffer)[0];
i = 0x5FE6EC85E7DE30DAn - (i >> 1n);
let f = new Float64Array(new BigInt64Array([i]).buffer)[0];
f = f * (1.5 - xHalf * f * f);
f = f * (1.5 - xHalf * f * f);
f = f * (1.5 - xHalf * f * f);
return Math.floor(1.0 / f);
};

View File

@ -1,5 +1,12 @@
package com.iqiaoxu.code;
import javafx.util.Pair;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
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));
@ -19,6 +26,10 @@ public class Solution {
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.subList(2, sum.size()).stream().reduce(new Pair<>(sum.get(0), Integer.max(sum.get(0), sum.get(1))), ((acc, x) -> new Pair<>(acc.getValue(), Integer.max(acc.getKey() + x, acc.getValue()))), (a, b) -> new Pair<>(0, 0)).getValue();
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 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]);
}
}