1 Writing plans
Recently in the "Jian Point offer", found that they have a lot of data structure and algorithm basic knowledge to review, "good Book read together (131): Let the writing better" in the mention of writing down forced reading , I very much agree. Therefore, the plan with "the sword refers to offer" as the center, in order to record review the intention, write a series of data structure and algorithm of the article.
- Article structure
The article starts from the concept introduction, then introduces the relevant language details (in C language and Python-based), and finally to the "sword-point offer" in the programming problem knot.
2 What is an array
When I mention the array, I first time the classic C language implementation and corresponding to a fixed length of contiguous memory space. The read and write operation of the array is fast and the time complexity is O (1), but the space utilization is not high because it must specify the length beforehand. The subscript of an array is the identification of the elements in the array, which can be used as an ID for an element, and a good array subscript should be used when large amounts of data need to be recorded.
3 C language Details
This section describes the necessary language details of the C language from the array in 3 sections.
3.1 Array out of bounds problem
C language does not force check array subscript is out of bounds, use should always pay attention to the cross-border problem.
This is a meaningless question because the C language does not allow arrays to be out of bounds at all.
Array out-of-bounds is a undefined behavior,c language does not specify the results of the program run, and the compiler does not guarantee this result. The C language stipulates that this error compiler does not have to point out, so the compilation may pass normally, but this is still a mistake, and the responsibility is coder.
So it's a meaningless question, just like asking what the socks stewed eggplant would taste. Only the idiot would cook the eggplant with his socks and try it himself-that's the essence of the answer.
Shifei
Links: https://www.zhihu.com/question/22897368/answer/22999166
Source: Know
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
3.2 Arrays and pointers
This section describes the relationship between arrays and pointers and the points of attention in using arrays.
The array name is a special pointer
This section describes the similarities and differences between arrays and pointers.
Take an array int a[10];
as an example:
int a[10];
int n = 10;
int a[n]; //定义数组时,长度不可以为变量,该语句非法
Common denominator: Accessing array elements through pointers
a[i]
*(a+i)
&a[i]
is equivalent to, and a+i
both.
- Accessing an array with an equivalent pointer
int *pa;
pa = &a[0]; //指针pa指向数组的0元素地址
pa = a; //这个写法和上句等价
x = *(pa+1);
x = a[1]; //这两句赋值语句等价
Different points: The difference between an array name and a pointer
The pointer is a variable, but the array name is not a variable.
int *pa;
pa = &a[0]; //指针pa是可以被赋值的,其指向地址可以改变
int b[10];
a = b; //a不可以被赋值,该语句非法
The array passed in the function is a pointer (address)
- Take the code in the offer of swords as an example:
int GetSize(int data[])
{
return sizeof(data);
}
int main()
{
int data1[] ={1, 2, 3, 4, 5};
int size1 = sizeof(data1); //此处求数组data1所占字节数
int *data2 = data1;
int size2 = sizeof(data2); //此处求指针data2所占字节数
int size3 = GetSize(data1); //data1的地址被赋值给了局部变量data,所以返回指针data所占字节数
printf("%d, %d, %d", size1, size2, size3);
}
//输出结果为:20, 4, 4
- Passing partial arrays
Using an array of attributes passed as a pointer, we can assign a function to the back part of the array. For f(&a[3])
example f(a+3)
, the second 7 elements of array A are passed to the function f.
3.3 Arrays and hash tables
The
Array can be seen as a simple hash table with a hash function of H ( k ) = k " Style= "font-size:100%; Display:inline-block; Position:relative "> , k "style=" font-size:100%; Display:inline-block; Position:relative "> is the keyword key for the element. The
commonly used hash functions are:
- Except Fahahi method
A prime number that is not too close to 2 for an integer power
- Multiplication Hash method
whichis to takeThe number of decimal parts, i.e.。
The recommended variable values are:;。
- Full Domain Hash method
Randomly select the hash function so that it is independent of the keyword to be stored. No detailed introduction is made here.
4 Programming Questions
# -*- coding:utf-8 -*-
class Solution:
# array 二维列表
def Find(self, target, array):
# write code here
r, c = len(array), len(array[0])
i, j = 0, c - 1 //选择合适的开始位置,右上角和左下角都可以
while (i < r and j >= 0):
if target < array[i][j]:
j = j - 1
elif target > array[i][j]:
i = i + 1
else:
return True
return False //之前将return放在while循环中,一直通不过
C Language:
C language Implementation and Python, the OJ system does not seem to support the C language, here skip.
5 Conclusion
As the hackers and painters say, programming languages are the way programmers think. The way to think about the problem is based on specific language, such as group, in C language thinking, I see a fixed length of continuous memory space; With Python thinking, I see a random sequence of arbitrary combinations of operations, learning different languages can understand the same concept from different angles, breaking their own thinking limitations.
Sword Point Offer (1): Array