LeetCode 刷题笔录 Add Two Numbers

中小团队必看!用Harbor+Jenkins打造轻量级K3S交付流水线(含成本优化方案) 本文为中小团队详细介绍了如何利用K3S、Harbor与Jenkins构建一套高性价比的自动化交付流水线。内容涵盖单节点K3S集群的效能配置、Harbor镜像仓库的磁盘空间优化、Jenkins共享库实现流水线复用,并提供了完整的实战案例与成本优化方案,帮助团队以较低成本快速落地高效的自动化部署。 阅读详情

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8


这题看起来没啥好说的,挨个顺序扫描就行了。要注意null的位置以及进位的问题。我的代码通不过测试。。。输入{9},{9}显示我的程序返回是{9},{1},而答案应该是{8},{1}。但是我把这代码在自己机器上用eclipse编译完了答案是正确的。。。也看不出自己哪里有错,可能是leetcode编译的问题吧。先记下来再说。

private boolean carry = false;      //set the flag to true if a carry is needed 
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        // Start typing your Java solution below
        // DO NOT write main() function
        int first = add(l1.val, l2.val);
        ListNode previous = new ListNode(first);      //the first node of the answer linked list
        ListNode header = previous;
        for(l1 = l1.next, l2 = l2.next; l1 != null && l2 != null; l1 = l1.next, l2 = l2.next){
            previous = previous.next = new ListNode(add(l1.val, l2.val));
        }
        if(l1 == null && l2 == null){
            if(carry){
                previous = previous.next = new ListNode(1);
            }
        }
        else if(l1 == null){
            while(l2 != null){
                previous = previous.next = new ListNode(l2.val);
                l2 = l2.next;
            }
        }
        else if(l2 == null){
            while(l1 != null){
                previous = previous.next = new ListNode(l1.val);
                l1 = l1.next;
            }
        }
        
        return header;
    }
    /**
     * add the two numbers
     * return the last digit of the result of the sum of the two numbers
     * if the sum is larger than 10, set the carry flag to true
     */
    private int add(int a, int b){
        int result = a + b;
        if(carry){
            result++;
        }
        if(result < 10){
            carry = false;
            return result;
        }
        else{
            result = result % 10;
            carry = true;
            return result;
        }
    }


AutoLisp文件(.lsp)从编写到加载运行:一份给CAD新手的避坑指南 本文为CAD新手提供了一份AutoLisp文件(.lsp)从编写到加载运行的完整指南,涵盖编写环境配置、文件加载流程、调试技巧及工程化实践建议。通过详细的步骤和常见错误解决方案,帮助用户避免常见陷阱,提升AutoLisp脚本的开发效率和应用效果。 阅读详情

相关推荐

Windows加域迁移避坑指南:Profwiz工具实战中的5个常见问题与解决方案

本文详细解析了使用Profwiz工具进行Windows加域迁移时的5个常见问题及解决方案,包括临时配置文件生成、密码策略冲突、SID冲突等。通过实战案例和优化技巧,帮助IT运维团队高效完成账户配置文件迁移,确保Active Directory环境下的数据完整性和权限无缝衔接。

q9r0s的博客 517

LeetCode刷题笔录 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. Fo

likecool21的专栏 1万+

Frigate进阶玩法:如何用MQTT和Python脚本扩展你的自定义目标检测算法

本文详细介绍了如何通过MQTT和Python脚本扩展Frigate的自定义目标检测算法,无需修改源码即可实现特殊场景(如工业设备异常、农业病虫害识别)的高效检测。文章涵盖Frigate配置、外部检测服务构建、高级集成技巧及性能优化实践,帮助开发者灵活扩展智能监控能力。

weixin_42662293的博客 348

LeetCode刷题笔录 Two Sum

Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the

likecool21的专栏 1万+

thinkphp5.0.24反序列化漏洞分析

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!因此收集整理了一份《Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

m0_67392182的博客 1871

FDTD仿真实战:从材料导入到反射率曲线分析

本文提供了一份完整的FDTD仿真实战指南,详细讲解了从自定义材料(如WO3)数据导入、结构建模、仿真区域与网格设置,到光源与监视器配置、运行仿真并提取反射率数据的全流程。重点解决了材料库缺失、网格优化、边界条件设置等常见难题,旨在帮助研究人员高效、准确地通过FDTD仿真获取材料的关键光学特性——反射率曲线。

elastic6hunter的博客 341

LeetCode刷题笔录 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key.Th

likecool21的专栏 6928

深度学习注意力机制新突破:YOLOv8结合SimAM无参数注意力模块,让目标检测更精准!

在计算机视觉领域,注意力机制已成为提升模型性能的关键技术。然而,传统的注意力模块如SENet、CBAM等往往引入大量额外参数,增加了模型的复杂度和计算负担。本文介绍一种创新的无参数注意力机制——SimAM(Simple Parameter-Free Attention Module),并将其成功应用于YOLOv8目标检测框架。SimAM基于神经科学理论,通过定义能量函数为特征图计算3D注意力权重,无需任何额外参数即可显著提升模型性能。本文详细阐述了SimAM的理论基础、实现原理,并提供了完整的YOLOv8集

YOLO 97

Stanford Algorithms学习:Clustering 2

这是紧接上面的一道题,比较有趣 Question 2 In this question your task is again to run the clustering algorithm from lecture, but on a MUCH bigger graph. So big, in fact, that the distances (i.e., edge costs

likecool21的专栏 2567

RH850 GreenHillSoftware Install

IDE一次链接到一个Compiler/Green Hills Debug Probe安装目录。如果您安装了IDE、编译器和绿山调试探测器软件,安装程序会将安装链接在一起。如果您只使用Advanced Install选项安装IDE,那么当您第一次启动它时,IDE会提示您链接到编译器和绿山调试探针安装*。如果您使用Advanced Install选项只安装编译器和绿山调试探针软件,安装程序会提示您将IDE的安装链接到新的编译器和绿山调试探针安装。

舊梦的博客 1214

机器学习分类算法之XGBoost(集成学习算法)

目录 走进XGBoost 什么是XGBoost? XGBoost树的定义 XGBoost核心算法 正则项:树的复杂程度 XGBoost与GBDT有什么不同 XGBoost需要注意的点 XGBoost重要参数详解 调参步骤及思想 XGBoost代码案例 相关性分析 n_estimators(学习曲线) max_depth(学习曲线) 调整max_depth 和min_child_weight 调整gamma 调整subsample 和colsample_bytree 调整正则

迷茫与徘徊只会让你陷入绝境,欢迎私信博主,带你开始提升变现价值! 11万+

Stanford Algorithms学习:Clustering

第二周的第一个编程作业,是利用贪婪算法来实现一个clustering的问题,和ML里学的unsupervised learning差不多。 Question 1 In this programming problem and the next you'll code up the clustering algorithm from lecture for computing a

likecool21的专栏 2507

六、Tool 工具系统:让大模型拥有“手脚“

我问你一个问题:大模型能不能告诉你"北京今天天气怎么样"?能——它能用非常流畅的中文回答你不能——它不知道这个答案因为它没有联网能力,也没有实时数据。它可能会自信地编一个"今天晴,25 度"给你——这就是幻觉。问题出在哪?大模型是一个"推理引擎",不是一个"执行引擎"。它知道天气 API 怎么调用,但不能真的发出 HTTP 请求。它知道 Python 代码怎么写,但不能真的运行代码。它知道数学公式,但做不了精确的大数计算。Tool 工具系统解决的,就是这个"最后一公里"问题。

牛肉胡辣汤 157

LeetCode刷题笔录Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word:

likecool21的专栏 1296

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

LeetCode刷题笔录Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

likecool21的专栏 1109

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

LeetCode刷题笔录 Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 这题比较简单

likecool21的专栏 1043

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

LeetCode刷题笔录Unique Binary Search Trees

这里有个讲解(印度人口音真难懂)

likecool21的专栏 1043

LeetCode刷题笔录Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin

likecool21的专栏 1001

LeetCode刷题笔录 Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 随手写的一个,正常的读出每一位的数字然后反序: import java.lang.Math; public class Solution { public int revers

likecool21的专栏 941

LeetCode刷题笔录Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the tota

likecool21的专栏 878

LeetCode刷题笔录Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the f

likecool21的专栏 865

LeetCode刷题笔录Convert Sorted Array to Balanced Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这题可以kan'chu'lai

likecool21的专栏 855

Spring AI + GPT-5.5 + Ollama 配置教程:Base URL、超时与流式调用

由于不同 Spring AI、Spring Boot 小版本之间仍可能存在差异,创建项目时应检查 Spring Initializr、Spring AI BOM 和官方兼容矩阵,不要直接复制旧教程中的 Starter 名称和版本号。多人项目最好隔离不同客户端,并在脱敏前提下记录最终 URL、状态码、请求耗时、请求 ID 和模型 ID。Spring AI 接入模型 API,至少要同时确认 Starter、Base URL、请求路径、API Key、模型 ID、连接超时、读取超时和流式客户端这 8 项。

2601_95893075的博客 224

【JVM】对象内存布局

HotSpot JVM 中,Java 对象内存布局由对象头(12字节,含Mark Word与压缩Klass Pointer)、实例数据及对齐填充构成,总大小为8字节倍数。对象头支持锁状态动态复用,指针压缩使引用仅4字节,但堆超32GB时失效。实例字段按类型优先级重排序以减少间隙,最小对象大小为16字节。通过JOL工具可精准验证布局,理解锁机制、GC、伪共享等底层行为。

2301_79732484的博客 149

V8 机制、手写核心函数与 TypeScript 类型体操

或固定初始化(如数组先定长、对象先给全字段)保持类型与形状稳定;这些都比"换框架"更基础。只能处理纯数据(丢函数、Date、RegExp、循环引用报错)。生产级深拷贝要处理:原始值短路、循环引用、V8(Chrome / Node 的 JS 引擎)不是"解释一行跑一行"那么简单,它用。图:V8 执行流程——解释执行 + 热点优化,优化失败会 Deopt 回退。JS 内存自动管理,但"自动"不等于"不会漏"。是"固定间隔最多执行一次"。手写 Promise 是"懂异步"的试金石。是"停下后才执行最后一次",

qq_38468358的博客 328

Spring Boot 数据源与 MyBatis(-Plus) 自定义配置详解

本文详解 Spring Boot 中单数据源环境下 HikariCP 与 MyBatis-Plus 的自定义配置。通过手动声明 SqlSessionFactory,实现下划线转驼峰、日志输出、一级缓存策略等精细化控制。结合 HikariCP 核心参数调优(如最大连接数、连接生命周期),确保高性能与稳定性。适用于仅需访问单一数据库的场景,避免多数据源复杂性,兼顾性能与可维护性。

BADAO_LIUMANG_QIZHI的博客 195
上一篇: Stanford Algorithms学习:Clustering 2
下一篇: Reverse A Linked List
likecool21
博客等级 码龄14年 14粉丝 123原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值