LC001

news/2024/10/17 6:29:38/

1.两数之和:‘

class Solution:def twoSum( nums, target):for i in nums:if target - i in nums:if i != target - i:return [nums.index(i), nums.index(target - i)]elif nums.count(i) > 1:index1 = nums.index(i)nums.remove(i)return [index1, nums.index(i) + 1]print(Solution.twoSum([1, 2, 4, 5], 6))
print(Solution.twoSum([3, 2, 4], 6))
print(Solution.twoSum([3, 3], 6))

class Solution2:def twoSum(nums: List[int], target: int) -> List[int]:dist={}for d,ind in enumerate(nums):dist[ind] = dfor i, j in enumerate(nums):f = dist.get(target - j)if f is not None and f != i:return [i, f]print(Solution2.twoSum([1, 2, 4, 5], 6))
print(Solution2.twoSum([3, 2, 4], 6))
print(Solution2.twoSum([3, 3], 6))
package addTwoNumbs;import java.util.HashMap;
import java.util.Map;public class Demo2 {public static int[] twoSum(int[] nums, int target) {Map<Integer, Integer> storeNum = new HashMap<Integer, Integer>();int[] result = new int[2];for (int i = 0; i < nums.length; i++) {int another = target - nums[i];Integer another1 = storeNum.get(another);if (null != another1) {result[0] = another1;result[1] = i;break;} else {storeNum.put(nums[i], i);}}return result;}public static void main(String[] args) {int[] a = new int[] { 4, 6, 13, 8, 7, 9 };Long start=System.currentTimeMillis();int[] b = twoSum(a, 20);for (int i = 0; i < 2; i++) {System.out.println(b[i]);}Long end=System.currentTimeMillis();System.out.println(end-start);}
}

2.两数相加:

class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = next
class Solution:def addTwoNumbers( l1: ListNode, l2: ListNode) -> ListNode:re = ListNode(0)r=recarry=0while(l1 or l2):x= l1.val if l1 else 0y= l2.val if l2 else 0s=carry+x+ycarry=s//10r.next=ListNode(s%10)r=r.nextif(l1!=None):l1=l1.nextif(l2!=None):l2=l2.nextif(carry>0):r.next=ListNode(1)return re.next
Definition for singly-linked list.public class ListNode {int val;ListNode next;ListNode(int x) { val = x; }}class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode result = new ListNode(0);add(result, l1, l2, 0);return result;}private void add(ListNode result, ListNode l1, ListNode l2, int carry) {//出口if (l1 == null && l2 == null && carry == 0) return;if (l1 == null) l1 = new ListNode(0);if (l2 == null) l2 = new ListNode(0);int num = l1.val + l2.val + carry;carry = num / 10;result.val = num % 10;//如果一直递归的话,最后会多出一个节点,所以需要再这里再加一个判断//如果满足条件就停止递归if (l1.next == null && l2.next == null && carry == 0) return;//递归result.next = new ListNode(0);add(result.next, l1.next, l2.next, carry);}
}


http://www.ppmy.cn/news/126507.html

相关文章

AT680A 超级电容漏电流测试仪 技术参数

AT680A 是采用高性能微处理器控制的超级电容漏电流测试仪。数控测试电压&#xff1a;1.000V~10.000V&#xff0c;充电电流2A&#xff0c;4量程测试&#xff0c;使漏电流准确度提高到1%&#xff0c;它可以测试0.001μA~200mA的电流&#xff0c;显示位数9999数。测试速度可达55次…

L1-006

L1-006 连续因子 (20分) 一个正整数 N 的因子中可能存在若干连续的数字。例如 630 可以分解为 3567&#xff0c;其中 5、6、7 就是 3 个连续的数字。给定任一正整数 N&#xff0c;要求编写程序求出最长连续因子的个数&#xff0c;并输出最小的连续因子序列。输入格式&#xff1…

Telerik 2020 R1 SP1 All Components -完整更新版

Telerik 专注于微软.Net平台的表示层与内容管理控件&#xff0c;提供高度稳定性和丰富性能的组件产品DevCraft&#xff0c;并可应用在非常严格的环境中。Telerik拥有 Microsoft, HP, Alcoa, BP, Harper Collins, Siemens, T-Mobile, HJ Heinz和一些最主要的教育机构和很多政府机…

B1001

害死人不偿命的(3n1)猜想 (15分) n%2&#xff01;0代表是奇数&#xff0c;if的判断条件里&#xff01;0可省略。 代码&#xff1a; #include<cstdio>int main() {int count 0, n;scanf("%d", &n);while(n!1){if(n%2) n (3*n1)/2;else n / 2;count;}p…

2.1 rvos简介

rvos简介 1 简介课程地址源码地址课程交流群 1 简介 rvos&#xff08;RISC-V OS&#xff09;是基于riscv开发的一个简单的适合入门的实时操作内核。 其作者是汪辰老师&#xff0c;有对应的源码及视频。 汪辰老师的这么课程讲的非常好&#xff0c;强烈推荐大家去观看 课程地址…

1011 -- RLE解码

RLE解码 Time Limit:1000MS Memory Limit:132768K Total Submit:224 Accepted:61 Description 在计算机中&#xff0c;图像通常是用矩阵表示的&#xff0c;矩阵中的每一个元素表示了图像中在相应位置上的像素值。而为了节约空间&#xff0c;图像文件一般都不是直接存储在外…

Qt下QTcpServer服务端识别多个QTcpSocket客户端

文章目录 Qt官方文档编写QTcpServerDemo和QTcpSocketDemo实现QTcpServerDemo实现QTcpSocketDemo 使用windeployqt生成程序运行所需依赖文件 Qt官方文档 QTcpSocket Class &#xff1a;https://doc.qt.io/qt-5/qtcpsocket.html QAbstractSocket Class&#xff1a;https://doc.q…

FR107对应 RS1M

FR307对应 RS3M FR107对应 RS1M 1N4007对应 M7 5KP48 功率5KW&#xff0c;截止电压VBR48V 5KP33 功率5KW,截止电压VBR33V