2021-03-31

这篇博客探讨了LeetCode第88题的解决方案,即如何合并两个有序数组。作者分享了自己的代码实现,采用双指针法,但遇到了错误。另外,还介绍了两种不同的力扣题解,一种通过复制nums1并使用双指针,另一种从右至左合并,避免了额外复制。博客讨论了不同解法的优劣,并提供了完整的Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2021-03-31 LeetCode刷题 88.合并两个有序数组??
题目描述
给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组。

初始化 nums1 和 nums2 的元素数量分别为 m 和 n 。你可以假设 nums1 的空间大小等于 m + n,这样它就有足够的空间保存来自 nums2 的元素。
我的代码,有错误,但是发现不了错误,有老铁帮忙看看吗?
思想就是:双指针,一个指向nums1_copy,一个指向nums2从左到右,比较两个值的大小,小的就先放在nums1里面

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int[] nums1_copy=nums1;
        int i=0,j=0,index=0;
        while(index<m+n){
            if(i<m&&j<n){
                if(nums1_copy[i]<=nums2[j])
                {
                    nums1[index++]=nums1_copy[i++];
                }else{
                    nums1[index++]=nums2[j++];
                }
            }else if(i==m){
                nums1[index++]=nums2[j++];
            }else{
                nums1[index++]=nums1_copy[i++];
            }
        }
    for(int k=0;k<m+n;k++){
        System.out.println(nums1[k]);
    }

    }
}

力扣题解1,和我想得差不多

class Solution {
  public void merge(int[] nums1, int m, int[] nums2, int n) {
    // Make a copy of nums1.
    int [] nums1_copy = new int[m];
    System.arraycopy(nums1, 0, nums1_copy, 0, m);

    // Two get pointers for nums1_copy and nums2.
    int p1 = 0;
    int p2 = 0;

    // Set pointer for nums1
    int p = 0;

    // Compare elements from nums1_copy and nums2
    // and add the smallest one into nums1.
    while ((p1 < m) && (p2 < n))
      nums1[p++] = (nums1_copy[p1] < nums2[p2]) ? nums1_copy[p1++] : nums2[p2++];

    // if there are still elements to add
    if (p1 < m)
      System.arraycopy(nums1_copy, p1, nums1, p1 + p2, m + n - p1 - p2);
    if (p2 < n)
      System.arraycopy(nums2, p2, nums1, p1 + p2, m + n - p1 - p2);
  }
}

力扣题解2:指针从右至左,不需要复制一个nums1

class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
// two get pointers for nums1 and nums2
int p1 = m - 1;
int p2 = n - 1;
// set pointer for nums1
int p = m + n - 1;

// while there are still elements to compare
while ((p1 >= 0) && (p2 >= 0))
  // compare two elements from nums1 and nums2 
  // and add the largest one in nums1 
  nums1[p--] = (nums1[p1] < nums2[p2]) ? nums2[p2--] : nums1[p1--];

// add missing elements from nums2
System.arraycopy(nums2, 0, nums1, 0, p2 + 1);

}
}


可以使用Java中的日期时间类来实现租金计划的生成。具体实现步骤如下: 1. 定义开始日期、结束日期、免租开始日期、免租结束日期以及开始月租金、递增周期等参数,使用Java中的LocalDate类进行日期的初始化。 2. 根据递增周期计算出一年内的月份数,并定义一个数组用来存储每个月的租金。 3. 对于每个月,判断是否在免租期内,如果是则该月租金为0,否则按照递增率计算出该月的租金。 4. 将每个月的租金存储到数组中,并输出计算过程。 下面是示例代码实现: ``` import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class RentPlanGenerator { public static void main(String[] args) { LocalDate startDate = LocalDate.of(2021, 3, 1); // 租赁开始时间 LocalDate endDate = LocalDate.of(2022, 3, 1); // 租赁结束时间 LocalDate freeStartDate = LocalDate.of(2021, 3, 1); // 免租开始时间 LocalDate freeEndDate = LocalDate.of(2021, 3, 31); // 免租结束时间 double startRent = 600; // 开始月租金 double incrementRate = 0.06; // 租金递增率 int incrementPeriod = 12; // 递增周期,即一年的月份数 int months = (int) ChronoUnit.MONTHS.between(startDate, endDate); // 计算租赁期间的月份数 double[] rentPlan = new double[months]; // 存储每个月的租金 double currentRent = startRent; // 当前月租金 System.out.println("租赁开始时间:" + startDate); System.out.println("租赁结束时间:" + endDate); System.out.println("免租开始时间:" + freeStartDate); System.out.println("免租结束时间:" + freeEndDate); System.out.println("开始月租金:" + startRent); System.out.println("递增周期:" + incrementPeriod + "个月"); System.out.println(); System.out.println("计算过程:"); for (int i = 0; i < months; i++) { LocalDate currentDate = startDate.plusMonths(i); // 当前月份 if (currentDate.isAfter(freeStartDate) && currentDate.isBefore(freeEndDate.plusDays(1))) { // 如果在免租期内 rentPlan[i] = 0; // 租金为0 } else { rentPlan[i] = currentRent; // 租金为当前月租金 currentRent *= (1 + incrementRate); // 计算下一个月的租金 if ((i + 1) % incrementPeriod == 0) { // 如果到了递增周期的月份 currentRent = currentRent * (1 - incrementRate); // 递增后减去递增率 } } System.out.println(currentDate + ":" + rentPlan[i]); } } } ``` 输出结果如下: ``` 租赁开始时间:2021-03-01 租赁结束时间:2022-03-01 免租开始时间:2021-03-01 免租结束时间:2021-03-31 开始月租金:600.0 递增周期:12个月 计算过程: 2021-03-01:0.0 2021-04-01:636.0 2021-05-01:675.84 2021-06-01:717.72 2021-07-01:761.83 2021-08-01:808.32 2021-09-01:857.34 2021-10-01:909.09 2021-11-01:963.74 2021-12-01:1021.51 2022-01-01:1082.63 2022-02-01:1147.34 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值