Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How C++ deletes elements with a value of 0

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to delete C++ and the element for 0", so the editor summarizes the following contents for you. The content is detailed, the steps are clear, and it has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how C++ deletes and zero elements" article.

Brief introduction of the algorithm:

By using the method of prefix sum, such as prefix and [3pyrrine 5pyrr6], then the sum of the nodes between the first 3 and the last 3 is 0, otherwise the two numbers cannot be equal.

/ / 1. Based on the above principle, we use the method of hash twice, and the first time hash finds the node / / 2 in the last location of the same prefix and sum. The second time hash finds the same node location as it appears for the first time, then delete all checkpoints between the two nodes.

Topic: delete continuous nodes with a comprehensive value of 0 from the linked list

Code implementation:

/ * Definition for singly-linked list. * type ListNode struct {* Val int * Next * ListNode *} * / func removeZeroSumSublists (head * ListNode) * ListNode {M1: = make (map [int] * ListNode) pre: = new (ListNode) pre.Next = head sum: = 0 for nlazomnil N=n.Next {sum + = n.Val M1 [sum] = n / / Save the contents of the same node in the last location} s: = 0 for mpurl prep; m! = nil M=m.Next {s + = m.Val m.Next = M1 [s] .Next / / Delete the node content between the first occurrence and the last occurrence of the same sum} return pre.Next} the above is about "how C++ deletes and zero elements". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report