π’ preface
π Algorithm problem π |
- π² Punching out an algorithm problem every day is not only a learning process, but also a sharing process π
- π² Tip: the problem-solving programming languages in this column are C# and Java
- π² To maintain a state of learning every day, let's work together to become the great God of algorithm π§!
- π² Today is the 38th day of punching out the force deduction algorithm π!
π Algorithm problem π |
π² Example of original question: circular linked list
Given a linked list, judge whether there are links in the linked list.
If there is a node in the linked list that can be reached again by continuously tracking the next pointer, there is a ring in the linked list. In order to represent the rings in a given list, we use the integer pos to represent the position where the tail of the list is connected to the list (the index starts from 0). If pos is - 1, there is no ring in the linked list.
Note: pos is not passed as a parameter, but only to identify the actual situation of the linked list.
Returns true if there are links in the linked list. Otherwise, false is returned.
Example 1:
Input: head = [3,2,0,-4], pos = 1 Output: true Explanation: there is a ring in the linked list, and its tail is connected to the second node.
Example 2:
Input: head = [1,2], pos = 0 Output: true Explanation: there is a ring in the linked list, and its tail is connected to the first node.
Example 3:
Input: head = [1], pos = -1 Output: false Explanation: there are no links in the linked list.
Tips:
- The number range of nodes in the linked list is [0, 104]
- -105 <= Node.val <= 105
- pos is - 1 or a valid index in the linked list
π» C# method: double pointer
Train of thought analysis
The fast and slow pointer starts from the node at the same time. The fast pointer takes two steps at a time and the slow pointer takes one step at a time. If there is a ring, the fast pointer will catch up with the slow pointer sooner or later.
code:
public class Solution { public bool HasCycle(ListNode head) { ListNode slow = head; ListNode fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if(slow == fast) { return true; } } return false; } }
results of enforcement
adopt Execution time: 92 msοΌAt all C# Defeated 74.50% of users in submission Memory consumption: 26.6 MBοΌAt all C# Beat 75.17% of users in submission
π» Java method: hash table
Train of thought analysis
The easiest way to think of is to traverse all nodes. Each time you traverse a node, judge whether the node has been accessed before.
Specifically, we can use a hash table to store all the nodes that have been accessed.
Every time we reach a node, if the node already exists in the hash table, it means that the linked list is a ring linked list, otherwise the node will be added to the hash table. Repeat this process until we traverse the entire linked list.
code:
class Solution { public boolean isPalindrome(String s) { StringBuffer sgood = new StringBuffer(); int length = s.length(); for (int i = 0; i < length; i++) { char ch = s.charAt(i); if (Character.isLetterOrDigit(ch)) { sgood.append(Character.toLowerCase(ch)); } } StringBuffer sgood_rev = new StringBuffer(sgood).reverse(); return sgood.toString().equals(sgood_rev.toString()); } }
results of enforcement
adopt Execution time: 4 msοΌAt all Java Defeated 20 in submission.23%User Memory consumption: 39.1 MBοΌAt all Java Defeated 83 in submission.75%User
Complexity analysis
Time complexity: O( N ),among N Is the number of nodes in the linked list Space complexity: O( N )οΌamong N Is the number of nodes in the linked list
π¬ summary
- Today is the 38th day of punch in of force deduction algorithm!
- This paper uses C# and Java programming languages to solve problems
- Some methods are also written by Likou God, and they are also shared while learning. Thanks again to the algorithm bosses
- That's the end of today's algorithm sharing. See you tomorrow!
π Previous high-quality article sharing
- β€οΈUnity zero foundation to entry | systematic learning route of game engine unity from 0 to 1 [comprehensive summary - suggestions collection]!
- π§‘Spend a day doing a high-quality aircraft war game and have a complete tutorial of 10000 words Unity! Beautiful Xuemei looked and called 666!
- πBack to childhood classic series βοΈ| [bomb man game] production process + analysis | collect it and dream back to childhood with your former friends!
- πA first person shooting game Demo similar to CS made all night! It's not difficult to play games
- π€Explosive liver wrote a real-time combat game Demo similar to the Royal war all weekend! More than 20000 word game production process + analysis!
- πHow to make a horizontal arcade fighting game similar to "dinosaur quick fight"| Learn together and send the source code by the way [code text is not easy, it is recommended to collect and learn]
- πBack to childhood classic series βοΈ| [Snake game] nearly 20000 words complete production process + analysis + source code [recommended collection and learning]