指定排序的数组中消重获取数组的剩余长度
public class SortArrayDistinct {
public static void main(String[] args) {
Integer[] nums = new Integer[] { 0, 1, 3, 3, 5, 8, 8, 8, 9 };
System.out.println(distinct(nums));
}
private static int distinct(Integer[] nums) {
int slow = 0;
Integer count = nums.length;
for (int quickPoint = 1; quickPoint < count; quickPoint++) {
if (nums[slow] != nums[quickPoint]) {
slow++;
nums[slow] = nums[quickPoint];
}
}
return slow + 1;
}
}
寻找数组的中心下标
public class MedianFind {
public static void main(String[] args) {
Integer[] nums = new Integer[] { 9, 7, 3, 1, 2, 1, 6, 4, 9 };
System.out.println(median(nums));
}
private static int median(Integer[] nums) {
Integer sum = Arrays.stream(nums).reduce(0, Integer::sum);
Integer leftSum = 0;
for (int i = 0; i < nums.length; i++) {
leftSum += nums[i];
if (leftSum == sum) {
return i;
}
sum -= nums[i];
}
return -1;
}
}
斐波那契数列
import java.util.Objects;
//斐波那契数列
public class FibonacciSequence {
//0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
public static void main(String[] args) {
//输出第n位数为?第11位数字,下标为10
System.out.println(calculation(10));
System.out.println(calculation1(10));
}
//--------------------第二种方法---------------------
private static Integer calculation1(int n) {
//结果集存放 为什么结果集的下标比入参下标大1
Integer[] result = new Integer[n + 1];
return recursion1(n, result);
}
//递归
public static Integer recursion1(int n, Integer[] result) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
if (Objects.nonNull(result[n]) && result[n] != 0){
return result[n];
}
result[n] = recursion1(n - 1, result) + recursion1(n - 2, result);
return result[n];
}
//-------------------------------------------------------
//--------------------第一种方法---------------------
private static Integer calculation(int n) {
return recursion(n);
}
//递归
public static Integer recursion(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
return recursion(n - 1) + recursion(n - 2);
}
//-------------------------------------------------------
}
有序数组中求三个数的最大积
public class SortArrayThreeMultiply {
public static void main(String[] args) {
Integer[] nums = new Integer[] { -10, -3, -2, -1};
System.out.println(multiply(nums));
}
//1:取三个最大值
//2:取两个最小值,一个最大值
//三个数最大的乘积一定在这两组数据中产生
//min1 min2 ..........max1 max2 max3
private static int multiply(Integer[] nums) {
Integer min1 = Integer.MAX_VALUE;
Integer min2 = Integer.MAX_VALUE;
Integer max1 = Integer.MIN_VALUE;
Integer max2 = Integer.MIN_VALUE;
Integer max3 = Integer.MIN_VALUE;
for (int i = 0; i < nums.length; i++) {
//判断最小值
Integer value = nums[i];
if (value < min1){
min2 = min1;
min1 = value;
}else if (value < min2){
min2 = value;
}
//判断最大值
if (value > max3){
max1 = max2;
max2 = max3;
max3 = value;
}else if (value > max2){
max1 = max2;
max2 = value;
}else if (value > max1){
max1 = value;
}
}
return Math.max(min1 * min2 * max3, max1 * max2 * max3);
}
}
硬币规则排列
//第一行放1,第二行放2,第n行放n,判断n枚硬币可以放满几行
public class RegularArrangementOfCoins {
public static void main(String[] args) {
//判断n枚硬币可以放满几行
// System.out.println(calculation(22));
System.out.println(calculation1(22));
}
private static int calculation1(int n) {
//定义剩余硬币
int x = n;
//n枚硬币铺满的行树绝对不会大与n
for (int i = 1; i <= n; i++) {
if (x < i){
//剩余硬币铺不满i行时,返回
return i - 1;
}
//更新剩余硬币
x = x - i;
}
return -2;
}
//1+2+3.......+N=(n+1)n/2
private static Integer calculation(int n) {
//n个硬币肯定不会放满n行,使用二分法不断逼近正确的数据
int first = 1;
int last = n;
while (first <= last){
//获取二分位
int mid = (last - first)/2 + first;
//判断二分位枚硬币的总数
int count = (mid + 1) * mid/2;
if (count == n){
return mid;
}
if (count < n){
//左指针右移
first = mid + 1;
}
if (count > n){
//右指针左移
last = mid - 1;
}
}
return 0;
}
}
求指定数平方根的整数位
public class SquareRootIntegerBit {
public static void main(String[] args) {
System.out.println(calculation(25));
}
//牛顿迭代
private static int calculation(int n) {
// x^2 = n 则 x = n/x
//思路:两个数的均值一定更最接近目标值 当两个值相等的时候,则为目标结果
int aim = n;
double recursion = recursion(aim, n);
return (int) recursion;
}
private static double recursion(double aim, int n) {
double i = (aim + n / aim) / 2;
if (i == aim){
return aim;
}else {
return recursion(i, n);
}
}
}