Pointer
It includes python, but not just python, but also C, Java, any programming language
Pointers are an essential part of a programming language, and if you don’t use them correctly, you’re basically losing all the functionality and flexibility that programming languages allow.Pointer is not only a language foundation or characteristic, but also an idea. if you want learning well.
The pointer can be understood as a bridge of communication,So,It’s really important!
A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type.
The unary or monadic operator & gives the ``address of a variable’’.
The indirection or dereference operator * gives the ``contents of an object pointed to by a pointer’’.
Oh,It has so many uses
The use of the foundation can be found in other articles. I will use the pointer or pointer thought to accomplish something
Answer source code,Suggest doing it first
Given an array of integers
nums
and an integertarget
, return indices of the two numbers such that they add up totarget
. You may assume that each input would have exactly\ one solution, and you may not use the same element twice. You can return the answer in any order.
# Method 1
'''
Tow Pointer,Nested loop.
'''
# Pseudo code
for loop -> i(nums.index)
for loop -> j(nums.index)
if nums[i] + num[j] == target and nums[i] != nums[j]:
# two pointer: i, j
return i, j
# Method 2
'''
Because of this relationship: nums[i] + nums[j] == target
therefore: nums[i] = target - nums[j]
'''
# Pseudo code
for loop -> i (nums.index)
a = target - nums[i]
if a in nums and nums.index(a) != i:
return i, nums.index(a)
# Method 3 - other ideas
'''
Pursuing speed
You might think about the relationship between index and number
if you know dcit from key -> values find is fast, you maybe use it
'''
new HashMap
index -> Value - > index
Given an array
nums
, write a function to move all0
‘s to the end of it while maintaining the relative order of the non-zero elements. Note:
# two pointers slow and fast: -> i, j
i -> start
j -> for loop
when j != 0, Move it to the front that from nums
Given
head
, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following thenext
pointer. Internally,pos
is used to denote the index of the node that tail’snext
pointer is connected to. Note thatpos
is not passed as a parameter. Returntrue
if there is a cycle in the linked list. Otherwise, returnfalse
. Follow up: Can you solve it usingO(1)
(i.e. constant) memory? Constraints:
[0, 104]
.-105 <= Node.val <= 105
pos
is -1
or a valid index in the linked-list.# two pointer: slow and fast
when slow == fast
return
Pointers are basic, but flexible. I believe you can also learn its flexible characteristics, as well as the idea of pointer. All changes do not depart from their ancestorsBelieve me, you can, too.