C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
int numComponents(ListNode* head, vector<int>& nums) {
}
};
运行代码
提交
python3 解法, 执行用时: 76 ms, 内存消耗: 20.3 MB, 提交时间: 2022-12-05 15:50:50
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def numComponents(self, head: Optional[ListNode], nums: List[int]) -> int:
numsSet = set(nums)
inSet = False
res = 0
while head:
if head.val not in numsSet:
inSet = False
elif not inSet:
inSet = True
res += 1
head = head.next
return res
golang 解法, 执行用时: 20 ms, 内存消耗: 6.3 MB, 提交时间: 2022-12-05 15:50:24
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func numComponents(head *ListNode, nums []int) (ans int) {
set := make(map[int]struct{}, len(nums))
for _, v := range nums {
set[v] = struct{}{}
}
for inSet := false; head != nil; head = head.Next {
if _, ok := set[head.Val]; !ok {
inSet = false
} else if !inSet {
inSet = true
ans++
}
}
return
}
javascript 解法, 执行用时: 72 ms, 内存消耗: 46.7 MB, 提交时间: 2022-12-05 15:50:10
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number[]} nums
* @return {number}
*/
var numComponents = function(head, nums) {
const numsSet = new Set();
for (const num of nums) {
numsSet.add(num);
}
let inSet = false;
let res = 0;
while (head) {
if (numsSet.has(head.val)) {
if (!inSet) {
inSet = true;
res++;
}
} else {
inSet = false;
}
head = head.next;
}
return res;
};
java 解法, 执行用时: 5 ms, 内存消耗: 42.2 MB, 提交时间: 2022-12-05 15:49:52
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public int numComponents(ListNode head, int[] nums) {
Set<Integer> numsSet = new HashSet<Integer>();
for (int num : nums) {
numsSet.add(num);
}
boolean inSet = false;
int res = 0;
while (head != null) {
if (numsSet.contains(head.val)) {
if (!inSet) {
inSet = true;
res++;
}
} else {
inSet = false;
}
head = head.next;
}
return res;
}
}