LeetCode刷题day016 (Jieky)

LeetCode第16题

/*
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:
Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
*/
import java.util.*;
public class ThreeSumClosest{
	// https://njucz.github.io/2017/08/16/java-int%E6%BA%A2%E5%87%BA%E6%80%BB%E7%BB%93/
	// https://blog.csdn.net/a327369238/article/details/52354811
	// https://blog.csdn.net/a327369238/article/details/52238102
	public static void main(String[] args){
		// int[] nums = {1,1,-1,-1,3};
		// int target = -1;
		int[] nums = {Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE};
		int target = Integer.MIN_VALUE;
		ThreeSumClosest tsc = new ThreeSumClosest();
		int result = tsc.threeSumClosest02(nums,target);
		System.out.println(result);
		
		System.out.println("-----------------------------------------");
		int num = 907654321;
		System.out.println(num * 16);
		// int -> long 没有溢出
		System.out.println(num * 16L);
		// int型整数相乘,结果只会保留低32位,高位会抛弃掉
		System.out.println((int)((num * 16L) & 0xffffffff));
		
		int num1 = Integer.MAX_VALUE;
		System.out.println(num1 + 1);
		System.out.println(num1 + 1L);
		// int型整数相加,结果只会保留低32位,高位会抛弃掉
		System.out.println((int)((num1 + 1L) & 0xffffffff));
		
		System.out.println("-----------------------------------------");
		// 在计算右值的过程中(int型相乘)发生溢出,然后将溢出后截断的值赋给变量
		long MonthNanoSeconds1 = 30 * 24 * 3600 * 1000 * 1000;
		System.out.println(MonthNanoSeconds1);
		// 在与最后一个long型的1000相乘之前就已经溢出,所以结果也不对
		long MonthNanoSeconds2 = 30 * 24 * 3600 * 1000 * 1000L;
		System.out.println(MonthNanoSeconds2);
		// 正确的打开方式
		long MonthNanoSeconds3 = 30L * 24 * 3600 * 1000 * 1000;
		System.out.println(MonthNanoSeconds3);

		System.out.println("-----------------------------------------");
		/*
		If any of the operands is of a reference type, unboxing conversion(JSL 5.1.8) is performed. Then:
		If either operand is of type double, the other is converted to double.
		Otherwise, if either operand is of type float, the other is converted to float.
		Otherwise, if either operand is of type long, the other is converted to long.
		Otherwise, both operands are converted to type int.
		*/
		byte a = 40;
		byte b = 50;
		byte c = 100;
		int d = a * b / c;
		System.out.println(d);
	}
	
	// 暴力破解,这也是不行的。无解
	public int threeSumClosest02(int[] nums, int target){
		int sub = Integer.MAX_VALUE; //保存和 target 的差值
        int sum = 0; //保存当前最接近 target 的三个数的和
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++)
                for (int k = j + 1; k < nums.length; k++) {
					/*
					int[] nums = {Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE};
					int target = Integer.MIN_VALUE;
					以上输入会溢出,此题有问题
					*/
                    if (Math.abs((nums[i] + nums[j] + nums[k] - target)) < sub) {
                        sum = nums[i] + nums[j] + nums[k];
                        sub = Math.abs(sum - target);
                    }
                }
        }
        return sum;
	}
	
	// 暴力破解
	public int threeSumClosest01(int[] nums, int target){
		int minSolution = Integer.MAX_VALUE;
		
		// 输入检查
		if (nums == null || nums.length < 3) return minSolution;
		
		int numsLen = nums.length;
		
		for(int i = 0;i<numsLen;i++){
			for(int j = i+1;j<numsLen;j++){
				for(int k=j+1;k<numsLen;k++){
					int sum = nums[i] + nums[j] + nums[k];
					// minSolution - target会有移除风险。(Integer.MAX_VALUE + 1) or (Integer.MAX_VALUE - (-1))
					if (Math.abs(sum - target) < Math.abs(minSolution - target)){
						minSolution = sum;
					}
				}
			}
		}
		return minSolution;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值