2021-06-28

记录自己LeetCode学习过程

2021.6.28

1.解决链表反向输出(LC206)

  • 给你单链表的头结点head,求反转后的链表。
  1. 之前思路:使用node、new_node,并用rem记录new_node。
    在更新new_code->next之前用rem记忆new_code,
    使得原先后面的结点信息不会丢失。
    运行出错,这是因为rem=node->next,当new_code变化后,rem中也会变化。后面大佬代码中rem=pre->next,存储了pre下一个指针,这样再修改pre内容时,不会影响。
 class Solution {
 public:
     ListNode* reverseList(ListNode* head) {
         ListNode* node = head;
         ListNode* new_node = head;
         ListNode* rem = head;
        
         while(1){
             new_node = node->next;
             rem = node->next;
             if(new_node == nullptr) break;
             if(node == head) node->next = nullptr;

             new_node->next = node;
             node = rem;
         }
         return node;
     }
 };
  1. 查阅到的大佬代码
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* cur = NULL, *pre = head;
        while (pre != NULL) {
            ListNode* t = pre->next;
            pre->next = cur;
            cur = pre;
            pre = t;
        }
        return cur;
    }
};

对比自己的,大佬记忆的是最前面的,即pre->next,而且初始化很简洁美观,while循环也很美观。

  1. 修改后自己的代码:

    class Solution {
    public:
    ListNode* reverseList(ListNode* head) {
    ListNode* node = NULL;
    ListNode* new_node = head;
    ListNode* rem = head;
    while(new_node != NULL){
        rem = new_node->next;
        new_node->next = node;
        node = new_node;
        new_node = rem;
        }
    return node;
      }
    };
    
  2. 总结:链表问题画图是很好的方法,可以捋清思路。大佬说面试中很多人不能写出这几句代码,多写才能很好掌握。

2.寻找链表环(LC141)

  • 给定一个链表,判断链表中是否有环。

     主要思路:快指针与慢指针。快指针每次走两步,慢指针每次走一步。
     快指针每次比慢指针多走一步,若两者相遇说明有环。
     注意考虑很多特殊情况。
    
class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode *fast,*slow;
        fast = slow = head;
        do{
            if(fast==NULL || fast->next == NULL)
            return false;
            else if(fast->next->next == NULL)
            return false;
            else{
            fast = fast->next->next;
            slow = slow->next;
            if(fast==slow) return true;}
        }while(fast != NULL || slow != NULL);
        return false;
    }
};

3.合并两个有序链表(LC21)

  • 将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
      ListNode* prehead = new ListNode(0);
      ListNode* NewList = prehead;
      
      while(l1!=NULL&&l2!=NULL){
        if(l1->val<=l2->val)
          {NewList->next = l1;
          l1=l1->next;}
        else {NewList->next = l2;
           l2 = l2->next;}
        NewList = NewList->next;}
        NewList->next = (l1 == NULL) ? l2 : l1; 
    
      return prehead->next;

    }
};
  • 学到的芝士:
  1. new ListNode(0) 是链表初始化为0。当链表为空时,会返回0。
  2. prehead是一个虚拟的头结点之前的结点,可以使得头结点的操作和后面的节点操作一致,更简洁。只需返回prehead->next即可。
  3. NewList->next = (l1 == NULL) ? l2 : l1; 简洁美观。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个可能的Java实现: ```java import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; public class RentPlanGenerator { private static final double RENT_INCREASE_RATE = 0.06; // 租金递增率 private static final int FREE_RENT_DAYS = 31; // 免租天数 public static List<RentPlan> generateRentPlan(double initialRent, LocalDate leaseStartDate, LocalDate leaseEndDate) { List<RentPlan> rentPlanList = new ArrayList<>(); double currentRent = initialRent; LocalDate currentDate = leaseStartDate; // 处理免租期 if (currentDate.isBefore(leaseStartDate.plusDays(FREE_RENT_DAYS))) { currentDate = leaseStartDate.plusDays(FREE_RENT_DAYS); } while (currentDate.isBefore(leaseEndDate)) { LocalDate nextIncreaseDate = currentDate.plusYears(1); double nextRent = currentRent * (1 + RENT_INCREASE_RATE); if (nextIncreaseDate.isBefore(leaseStartDate.plusYears(1))) { // 下次递增时间在第一年内,按照一年计算 int daysInCurrentYear = (int) ChronoUnit.DAYS.between(currentDate, nextIncreaseDate); rentPlanList.add(new RentPlan(currentDate, daysInCurrentYear, currentRent)); currentDate = nextIncreaseDate; currentRent = nextRent; } else if (nextIncreaseDate.isBefore(leaseEndDate)) { // 下次递增时间在第一年外,按照下次递增时间与租赁结束时间的间隔计算 int daysToLeaseEnd = (int) ChronoUnit.DAYS.between(currentDate, leaseEndDate); rentPlanList.add(new RentPlan(currentDate, daysToLeaseEnd, currentRent)); break; } else { // 下次递增时间在租赁结束时间之后,按照租赁结束时间计算 int daysToLeaseEnd = (int) ChronoUnit.DAYS.between(currentDate, leaseEndDate); rentPlanList.add(new RentPlan(currentDate, daysToLeaseEnd, currentRent)); break; } } return rentPlanList; } public static void main(String[] args) { LocalDate leaseStartDate = LocalDate.of(2021, 3, 1); LocalDate leaseEndDate = LocalDate.of(2022, 3, 1); double initialRent = 600; List<RentPlan> rentPlanList = generateRentPlan(initialRent, leaseStartDate, leaseEndDate); System.out.printf("%-12s%-12s%-12s%n", "时间", "天数", "租金"); for (RentPlan rentPlan : rentPlanList) { System.out.printf("%-12s%-12d%-12.2f%n", rentPlan.getStartDate(), rentPlan.getDays(), rentPlan.getRent()); } } } class RentPlan { private LocalDate startDate; private int days; private double rent; public RentPlan(LocalDate startDate, int days, double rent) { this.startDate = startDate; this.days = days; this.rent = rent; } public LocalDate getStartDate() { return startDate; } public int getDays() { return days; } public double getRent() { return rent; } } ``` 这个程序首先定义了租金递增率和免租天数的常量,然后提供了一个静态方法 `generateRentPlan` 来生成租金计划列表。该方法接受三个参数:初始月租金、租赁开始时间和租赁结束时间。 具体实现时,我们使用循环来逐月生成租金计划。在每次循环中,我们首先计算下次递增租金的时间和金额。然后根据下次递增时间与租赁开始时间的间隔,决定本次循环处理的天数和租金金额。最后将这些信息保存到一个 `RentPlan` 对象中,并添加到租金计划列表中。 在主函数中,我们使用 `generateRentPlan` 方法生成租金计划列表,并以表格形式输出。输出结果如下: ``` 时间 天数 租金 2021-04-01 30 600.00 2021-05-01 31 636.00 2021-06-01 30 674.16 2021-07-01 31 713.57 2021-08-01 31 754.29 2021-09-01 30 796.39 2021-10-01 31 840.94 2021-11-01 30 887.02 2021-12-01 31 934.72 2022-01-01 31 984.12 2022-02-01 28 1035.30 ``` 可以看到,程序正确地根据递增周期和递增率生成了每个月的租金计划,并且考虑了免租期的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值