怒刷leetcode的题目(1)237、104、136、100

本文详细介绍了C语言环境下解决算法与数据结构问题的方法,包括链表删除节点、二叉树深度计算、单数查找及树结构比较等经典案例。

https://leetcode.com/problemset/algorithms/上面的题目,每天做几道题目,大体从准确率高至低做下去

编程语言为c语言,因为跑的最快…


237 Delete Node in a Linked List 47.8% Easy
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
void deleteNode(struct ListNode* node) {
    if(node->next != NULL){
        node->val = node->next->val;
        node->next=node->next->next;   
    }
}
删除给定的节点,只需要将当前节点的val改成下个节点的值,这时候相当于有两个相同值的节点,再将当前节点的next指针指向下下个节点,也就是跳过第二个相同的节点就好了。



104 Maximum Depth of Binary Tree 45.1% Easy

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

 
int maxDepth(struct TreeNode* root) {
    if(root == NULL) // 递归出口  
        return 0;  
    int depthLeft = maxDepth(root->left);  
    int depthRight = maxDepth(root->right);  
    return depthLeft > depthRight ? (depthLeft + 1) : (depthRight + 1);  
}
求二叉树的深度,使用递归的思想,将root想做普通节点,任何节点的深度都是左右节点较深的那个值+1,除了树叶(深度为0,也是递归的结束条件)


136 Single Number 45.0% Medium

Given an array of integers, every element appears twice except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

int singleNumber(int* nums, int numsSize) {
        if (nums == 0 || numsSize < 1)  
            return 0;  
        int key = nums[0];  
        for (int i = 1; i < numsSize; ++i) {  
            key ^= nums[i];  
        }  
        return key;  
}
难点在于数组规模增大,运行时间只能线性增长。

将数组的所有元素进行异或运算,相同的元素异或等于0,唯一的元素与0异或等于自己。

100 Same Tree 41.5% Easy

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
    if(!p && !q) return true;  //NULL together, the same,skip the remaining code
    if(!p || !q) return false; //one is NULL but the other is not
    return (p->val == q->val) && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);     
}
跟104题类似,使用递归的思想,二叉树相同,意味着从根节点开始,必须符合对应节点的值相同+左节点为根节点的树相同+右节点为根节点的树相同

先判断!p&&!q同时为树叶节点,两个为相同的树

如果不同时为树叶节点,则判断两者是否有一方是树叶节点,如果是,则他们不是一样的树

对根节点运行算法就可以得出结果





### 在 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. 提交代码 完成题目后,可以直接通过插件将代码提交到 LeetCode1. 在代码编辑器中右键点击,选择 **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]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值