怒刷leetcode题目(3)226,83,142,86

本文解析了四道LeetCode题目:翻转二叉树、移除排序链表中的重复元素、寻找链表环的入口及链表分区问题。通过深入浅出的方式介绍了各题目的解决思路和代码实现。

226 Invert Binary Tree 36.2% Easy

Invert a binary tree.

     4
   /   \
  2     7
 / \   / \
1   3 6   9
to
     4
   /   \
  7     2
 / \   / \
9   6 3   1
对的,这就是前阵子homebrew大神面试没做出来的那道题

其实这道题并不难…也许只是大神不屑于做这样的题目罢了…


照样的,我们发现以下规律

所有的左子树和右子树交换,除非到了结点(树叶)

主需要交换两棵子树然后对子树递归就好了

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* invertTree(struct TreeNode* root) {
    if(root == NULL){
        return NULL;
    }
    struct TreeNode* temp = root->left;
    root->left=root->right;
    root->right = temp;
    invertTree(root->left);
    invertTree(root->right);
    return root;
}



83 Remove Duplicates from Sorted List 34.4% Easy

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

太简单了…都不知道说什么好了

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* deleteDuplicates(struct ListNode* head) {
    if(head == NULL){
        return false;
    }
    struct ListNode* pointer = head;
    while(pointer->next!=NULL){
        if(pointer->val==pointer->next->val){
            pointer->next = pointer->next->next;
        }else{
            pointer= pointer->next;
        }
    }
    return head;
}


142 Linked List Cycle II 31.4% Medium

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Follow up:
Can you solve it without using extra space?

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *detectCycle(struct ListNode *head) {
    if(head==NULL){
        return false;
    }
    struct ListNode *slow = head;
    struct ListNode *fast = head;
    while(fast!=NULL && fast->next!=NULL){
        slow = slow->next;
        fast = fast->next->next;
        if(slow == fast){
            slow = head;
            while(slow!=fast){
                slow = slow->next;
                fast = fast->next;
            }
            return slow;
        }
    }
    return false;
}




这道题目跟141(上一篇博客中提及的)很类似,都是找出环

142实际是升级版,要找出环的入口

同样是使用快慢指针,大家可以在纸上画一下,写一下

如果有环,第一次相遇的时候,慢指针走了L+X的路程(L是起点到环入口的距离,X是入口到相遇时走过的距离,距离有可能比一圈的长度大)

快指针想当然的走了(L+X)*2的路程(其实不一定是2,只不过2比较好计算而已)

而且!!!快指针走过的距离是L+X+m*R(L、X同上,毕竟相遇点相同,m代表的是走过的圈数,R代表圈的长度)

也就是L+X=m*R

所以L=m*R-X

这时候,我们将慢指针回到起点,快指针的每一步的距离变成1

我们可以知道,慢指针和快指针相遇的时候

慢指针走了L,快指针走了m*R+m*R+X-X=2*m*R,也是在环的入口

所以他们再次相遇的节点就是环的入口


86 Partition List 27.4% Medium

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

遍历一次,将节点放进相应的链表,最后相连就好了,注意细节比较重要

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* partition(struct ListNode* head, int x) {
    if(head==NULL){
        return NULL;
    }
    if(head->next==NULL){
        return head;
    }
    struct ListNode* less_list = NULL;
    struct ListNode* less_list_head = NULL;
    struct ListNode* greater_list = NULL;
    struct ListNode* greater_list_head = NULL;
    struct ListNode* pointer = head;
    while(head!=NULL){
        struct ListNode* next = head->next;
        head->next = NULL;
        if(head->val<x){
            if(less_list_head==NULL){
                less_list_head=head;
                less_list=head;
            }else{
                less_list->next = head;
                less_list=less_list->next;
            }
        }else{
            if(greater_list_head==NULL){
                greater_list_head=head;
                greater_list=head;
            }else{
                greater_list->next = head;
                greater_list=greater_list->next;
            }
        }
        head=next;
    }
    if(less_list_head==NULL){
        return greater_list_head;
    }
    
    less_list->next=greater_list_head;
    return less_list_head;
}






### 在 IntelliJ IDEA 中练习 LeetCode 编程题 IntelliJ IDEA 是一个功能强大的 Java 开发环境,通过使用插件可以方便地在本地 IDE 中直接练习 LeetCode 编程题。以下是详细的配置和使用方法。 #### 1. 安装 LeetCode 插件 首先,需要在 IntelliJ IDEA 中安装 LeetCode 揖件: 1. 打开 IntelliJ IDEA,进入 **Settings (设置)**(快捷键为 `Ctrl + Alt + S`)。 2. 在左侧菜单中选择 **Plugins (插件)**。 3. 在搜索框中输入 `LeetCode`。 4. 找到由社区开发的 LeetCode 插件,点击 **Install (安装)**。 5. 安装完成后重启 IntelliJ IDEA [^2]。 #### 2. 配置 LeetCode 插件 安装插件后,需要进行一些基本配置,以便插件能够正确连接到 LeetCode 账户并下载题目: 1. 重启 IntelliJ IDEA 后,在左侧边栏会看到一个 **LeetCode** 图标,点击它。 2. 点击登录按钮,进入 LeetCode 登录页面。 3. 在浏览器中打开 [LeetCode 网站](https://leetcode.com/),并登录你的账户。 4. 打开浏览器的开发者工具(通常可以通过按 `F12` 或 `Ctrl + Shift + I` 打开),在 **Application** 或 **Storage** 标签下找到 `LEETCODE_SESSION` 的 Cookie 值。 5. 将 Cookie 值复制并粘贴到 IntelliJ IDEA 插件的登录框中,完成登录 [^2]。 #### 3. 下载并练习 LeetCode 题目 登录成功后,可以在插件中浏览 LeetCode 上的所有题目,并选择相应的题目进行练习: 1. 在左侧边栏的 LeetCode 插件界面中,可以看到题目列表。 2. 右键点击某个题目,选择 **Show Problem (显示题目)**,插件会在右侧显示题目的详细描述。 3. 再次右键点击题目,选择 **Generate File (生成文件)**,插件会自动生成一个包含题目描述和类框架的 Java 文件。 4. 在生成的 Java 文件中编写代码,并使用插件提供的功能进行测试和调试 [^3]。 #### 4. 本地调试与测试 IntelliJ IDEA 提供了强大的调试功能,可以在本地对 LeetCode 题目进行调试: 1. 在代码编辑器中设置断点。 2. 右键点击代码编辑器中的类或方法,选择 **Debug (调试)**。 3. 插件会自动运行测试用例,并在断点处暂停,方便查看变量值和程序执行流程 [^3]。 #### 5. 提交代码 完成题目后,可以直接通过插件将代码提交到 LeetCode: 1. 在代码编辑器中右键点击,选择 **Submit (提交)**。 2. 插件会将代码提交到 LeetCode,并在插件界面中显示提交结果 [^1]。 #### 6. 代码保存与管理 IntelliJ IDEA 提供了强大的项目管理功能,可以方便地保存和管理 LeetCode 题目的代码: 1. 每道题目的代码都会保存在一个独立的文件中,文件名通常包含题目编号和题目名称。 2. 可以通过版本控制系统(如 Git)将代码保存到远程仓库中,方便后续查阅和复习 [^1]。 ### 示例代码 以下是一个简单的 LeetCode 题目示例代码,展示了如何在 IntelliJ IDEA 中编写和测试代码: ```java public class Solution { public int[] twoSum(int[] nums, int target) { for (int i = 0; i < nums.length; i++) { for (int j = i + 1; j < nums.length; j++) { if (nums[i] + nums[j] == target) { return new int[] {i, j}; } } } throw new IllegalArgumentException("No two sum solution"); } public static void main(String[] args) { Solution solution = new Solution(); int[] nums = {2, 7, 11, 15}; int target = 9; int[] result = solution.twoSum(nums, target); System.out.println("Result: [" + result[0] + ", " + result[1] + "]"); } } ``` 在 IntelliJ IDEA 中运行 `main` 方法,可以测试 `twoSum` 方法的正确性,并查看输出结果 [^3]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值