当前位置:网站首页>Leetcode learning records
Leetcode learning records
2022-06-28 07:34:00 【Little monkey who can't program】
1. Sum of two numbers
Sum of two numbers difficulty : Simple
Given an array of integers nums And an integer target value target, Please find... In the array And is the target value the Two Integers , And return their array subscripts .
Example 1:
Input :nums = [2,7,11,15], target = 9
Output :[0,1]
explain : because nums[0] + nums[1] == 9 , return [0, 1] .
Example 2:
Input :nums = [3,2,4], target = 6 Output :[1,2]
Example 3:
Input :nums = [3,3], target = 6 Output :[0,1]
Tips :
2 <= nums.length <= 103
-109 <= nums[i] <= 109
-109 <= target <= 109
There will only be one valid answer answer
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int sub = target - nums[i];
if (map.containsKey(sub)) {
return new int[]{map.get(sub), i};
}
map.put(nums[i], i);
}
return new int[]{-1, -1};
}
}2. Addition of two numbers
Addition of two numbers difficulty secondary
Here are two for you Non empty The linked list of , Represents two nonnegative integers . Each of them is based on The reverse Stored in , And each node can only store One digit . Please add up the two numbers , And returns a linked list representing sum in the same form .

Input :l1 = [2,4,3], l2 = [5,6,4]
Output :[7,0,8]
explain :342 + 465 = 807.
answer :
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode newHead = new ListNode(0);
ListNode p = l1, q = l2, cur = newHead;
int carry = 0;
while (p != null || q != null) {
int x = p == null ? 0 : p.val;
int y = q == null ? 0 : q.val;
int sum = carry + x + y;
carry = sum / 10;
cur.next = new ListNode(sum % 10);
cur = cur.next;
if (p != null) p = p.next;
if (q != null) q = q.next;
}
if (carry > 0)
cur.next = new ListNode(carry);
return newHead.next;
}
}
边栏推荐
- Using interceptor and cache to complete interface anti brushing operation
- R 语言 Kolmogorov-Smirnov 检验 2 个样本是否遵循相同的分布。
- Solving the longest palindrome substring by dynamic programming
- This keyword details
- 华为云计算之物理节点CNA安装教程
- 7-1 understand everything
- 剑指Offer||:链表(简单)
- Reading notes - MySQL technology l act: InnoDB storage engine (version 2)
- ice - 资源
- kubelet垃圾(退出的容器和未使用的镜像)回收源码分析
猜你喜欢

"Three routines" of digital collection market

MySQL installation steps - Linux configuration file JDK installation (II)

Modifying MySQL user name root under Linux

LeetCode+ 66 - 70 高精度、二分专题

Spark 离线开发框架设计与实现

ES6 use of return in arrow function

GoLand IDE and delve debug Go programs in kubernetes cluster

ACM笔记

全方位透析真实企业软件测试流程

云原生(待更新)
随机推荐
Devtools implementation principle and performance analysis practice
"Three routines" of digital collection market
Dbeaver 22.1.1 release, visual database management platform
Principle and practice of bytecode reference detection
Kubelet garbage collection (exiting containers and unused images) source code analysis
A small code editor can also run programs -- a summary of sublime Text3 running programs in various languages
ice - 资源
Introduction and several months' experience of extending the solution thanos of Prometheus
The practice of event driven architecture in vivo content platform
Design and implementation of spark offline development framework
okcc呼叫中心没有电脑的坐席能不能开展工作?
Will Internet talents be scarce in the future? Which technology directions are popular?
Uninstall and reinstall the latest version of MySQL database. The test is valid
小小一款代码编辑器竟然也可以有程序运行之功能——Sublime Text3运行各种语言程序的总结
What is EC blower fan?
7-1 understand everything
Leetcode+ 66 - 70 high precision, two sub topics
全方位透析真实企业软件测试流程
Is it safe for flush to open an account online
Modifying MySQL user name root under Linux