Sword Point Offer (1): Array

Source: Internet
Author: User
Tags ord

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:

 
   
  
  1. int a[10];
  2. int n = 10;
  3. 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
  
 
  1. int *pa;
  2. pa = &a[0]; //指针pa指向数组的0元素地址
  3. pa = a; //这个写法和上句等价
  4. x = *(pa+1);
  5. 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.

  
 
    1. int *pa;
    2. pa = &a[0]; //指针pa是可以被赋值的,其指向地址可以改变
    3. int b[10];
    4. a = b; //a不可以被赋值,该语句非法
The array passed in the function is a pointer (address)
    • Take the code in the offer of swords as an example:
  
 
  1. int GetSize(int data[])
  2. {
  3. return sizeof(data);
  4. }
  5. int main()
  6. {
  7. int data1[] ={1, 2, 3, 4, 5};
  8. int size1 = sizeof(data1); //此处求数组data1所占字节数
  9. int *data2 = data1;
  10. int size2 = sizeof(data2); //此处求指针data2所占字节数
  11. int size3 = GetSize(data1); //data1的地址被赋值给了局部变量data,所以返回指针data所占字节数
  12. printf("%d, %d, %d", size1, size2, size3);
  13. }
  14. //输出结果为: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:

  1. Except Fahahi method

    A prime number that is not too close to 2 for an integer power
  2. Multiplication Hash method

    whichis to takeThe number of decimal parts, i.e.。
    The recommended variable values are:;。
  3. 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
    • Online programming Resources
      New Ket Network Online programming

    • Finding in a two-dimensional array
      Python:

  
 
  1. # -*- coding:utf-8 -*-
  2. class Solution:
  3. # array 二维列表
  4. def Find(self, target, array):
  5. # write code here
  6. r, c = len(array), len(array[0])
  7. i, j = 0, c - 1 //选择合适的开始位置,右上角和左下角都可以
  8. while (i < r and j >= 0):
  9. if target < array[i][j]:
  10. j = j - 1
  11. elif target > array[i][j]:
  12. i = i + 1
  13. else:
  14. return True
  15. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.