LeetCode热题hot100

本文介绍了LeetCode热题hot100中的前五道题目,包括两数之和、两数相加、无重复字符的最长子串、寻找两个正序数组的中位数以及最长回文子串。
摘要由CSDN通过智能技术生成

LeetCode热题hot100

1. 两数之和

在这里插入图片描述
在这里插入图片描述

class Solution {
   
    public int[] twoSum(int[] nums, int target) {
   
        int n = nums.length;
        for (int i = 0; i < n; ++i) {
   
            for (int j = i + 1; j < n; ++j) {
   
                if (nums[i] + nums[j] == target) {
   
                    return new int[]{
   i, j};
                }
            }
        }
        return new int[0];
    }
}

2.两数相加

在这里插入图片描述
在这里插入图片描述

class Solution {
   
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
   
        ListNode head = null, tail = null;//声明一个链表, 给定头、尾结点
        int carry = 0;//进位
        while (l1 != null || l2 != null) {
   
       		 //若没有,默认为0
            int n1 = l1 != null ? l1.val : 0;
            int n2 = l2 != null ? l2.val : 0;
            int sum = n1 + n2 + carry;
			
            if (head == null) {
   //开始时
                head = tail = new ListNode(sum % 10);//尾结点指向头结点
            } else {
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值