leetcode day1

本文深入解析了LeetCode上的三道经典算法题:TwoSum问题,比较两棵二叉树是否相同,以及寻找最大子数组和。通过详细的代码示例,展示了如何使用哈希表解决TwoSum问题,如何递归地比较二叉树节点以确定树的结构和值是否相等,以及如何采用动态规划思想找到数组中具有最大和的连续子数组。

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

1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

 

talk is cheap,show me the code

 

class Solution {

                                                                                         

  public int[] twoSum(int nums[],int target)  {        

      HashMap<Integer,Integer> twoSum = new HashMap<>();                            

      for (int i=0;i<nums.length;i++) {                                           

          twoSum.put(nums[i],i);                                                    

      }                                                                             

      for (int i=0;i<nums.length-1;i++) {                                           

          int another = target-nums[i];                                             

          if (twoSum.containsKey(another) && i!=twoSum.get(another)) {              

              return new int[] {i,twoSum.get(another)};                             

          }                                                                         

      }                                                                             

        throw new IllegalArgumentException("no two num solution");

                                                                                      

  }                                                                                 

                                                                                      

     

}

 

100. Same Tree

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

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

Example 1:

Input:     1         1
          / \       / \
         2   3     2   3

        [1,2,3],   [1,2,3]Output: true

Example 2:

Input:     1         1
          /           \
         2             2

        [1,2],     [1,null,2]Output: false

Example 3:

Input:     1         1
          / \       / \
         2   1     1   2

        [1,2,1],   [1,1,2]Output: false

talk is cheap,let you see me code

/**

 * Definition for a binary tree node.

 * public class TreeNode {

 *     int val;

 *     TreeNode left;

 *     TreeNode right;

 *     TreeNode(int x) { val = x; }

 * }

 */

class Solution {

   public boolean isSameTree(TreeNode p, TreeNode q) {

      if (p == null && q == null) {                                                                                 

     return true;                                                                                              

 }                                                                                                             

 return p !=null && q !=null && p.val == q.val && isSameTree(p.left,q.left) && isSameTree(p.right,q.right);    

                                                                                                                 

    }

}

53. Maximum Subarray

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

 

 

talk is cheap show me the code

class Solution {

    public int maxSubArray(int[] nums) {    

     int sum = nums[0];                  

     int maxSum = sum;                   

     for (int i=1; i< nums.length;i++) { 

         if (sum >0) {                   

             sum = sum + nums[i];        

         else {                        

             sum = nums[i];              

         }                               

         if (sum > maxSum) {             

             maxSum = sum;               

         }                               

     }                                   

   return maxSum;                        

 }                                       

}

### LeetCode 积分获取方式 在 LeetCode 平台上,用户可以通过多种途径来增加自己的积分。完成每日题目能够使用户的分数有所增长[^1]。例如,在提到的一个实例中,“今日得分:+10 总得分:240”,这表明通过解决特定问题可以获得额外的积分奖励。 除了日常挑战外,参与周赛也是提升积分的有效手段之一。每周的比赛不仅考验参赛者的编程技巧,还提供了赢得更高排名的机会,从而带来更多的积分收益。不过需要注意的是,并未直接提及周赛的具体加分机制。 对于那些希望快速提高自己积分的人来说,专注于解答高难度的问题可能是一个不错的选择。通常来说,越难的任务完成后所给予的经验值也会相应增多。然而具体每道题目的确切分值并未在此处给出说明。 另外值得注意的是,持续活跃于社区讨论区也可能间接帮助个人积累更多积分。虽然这种活动本身并不直接贡献于积分系统,但是积极参与可以促进技能成长并发现更高效的解法路径,进而有助于更好地应对各类竞赛和练习中的难题。 ```sql -- 这里提供了一个SQL查询的例子用于计算玩家留存率,而非直接关联到积分制度, -- 但展示了如何利用数据库操作分析平台上的行为数据。 SELECT ROUND(COUNT(DISTINCT player_id) / (SELECT COUNT(DISTINCT player_id) FROM Activity), 2) AS fraction FROM Activity WHERE (player_id, DATE_SUB(event_date, INTERVAL 1 DAY)) IN ( SELECT player_id, MIN(event_date) FROM Activity GROUP BY player_id); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值