From 3d9454e25ec14d6778600c5335a19c3da543c1b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=B8=E7=94=B1?= Date: Wed, 14 Jul 2021 10:25:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9gitignore=E3=80=81213.?= =?UTF-8?q?=E6=89=93=E5=AE=B6=E5=8A=AB=E8=88=8DII=E7=9A=84java=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E3=80=81=E6=B7=BB=E5=8A=A0js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 19 ++++++++--------- JavaScript/my_leetcode.js | 27 +++++++++++++++++++++++++ java/src/com/iqiaoxu/code/Solution.java | 13 +++++++++++- 3 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 JavaScript/my_leetcode.js diff --git a/.gitignore b/.gitignore index c50a7e4..9757331 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ \ No newline at end of file diff --git a/JavaScript/my_leetcode.js b/JavaScript/my_leetcode.js new file mode 100644 index 0000000..2bf9022 --- /dev/null +++ b/JavaScript/my_leetcode.js @@ -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); +}; diff --git a/java/src/com/iqiaoxu/code/Solution.java b/java/src/com/iqiaoxu/code/Solution.java index ea884ab..e00e5f5 100644 --- a/java/src/com/iqiaoxu/code/Solution.java +++ b/java/src/com/iqiaoxu/code/Solution.java @@ -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 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]); } }