Home > Back-end > About the use of multidimensional pointer
About the use of multidimensional pointer
Time:10-07
Pure C due to work requirement, so the brush with C topic If inscribe http://https://leetcode-cn.com/problems/triangle/
The official answer key * * triangle int minimumTotal (int, int triangleSize, int * triangleColSize) { Int f [triangleSize] [triangleSize]; Memset (f 0, sizeof (f)); F [0] [0]=triangle [0] [0]. For (int I=1; I & lt; TriangleSize; + + I) { [I] [0]=f f [0] + [I - 1] triangle [I] [0]; For (int j=1; J & lt; i; + + j) { F [I] [j]=fmin (f [I - 1] [1], [I - 1) [j] f) + triangle [I] [j]; } [I] [I] f=f [I - 1] [I - 1) + triangle [I] [I]; } Int ret=f [triangleSize - 1] [0]; For (int I=1; I & lt; TriangleSize; I++) Ret=fmin (ret, f [triangleSize - 1] [I]); Return ret. }
Questions as follows: 1. The answer key to use the c language is a standard c? Why can use the variable initialization array length 2. The triangle is two-dimensional pointer, how is ability of triangle [j] operation [I]? Vs can't run, the reason is that the compiler doesn't know how much * triangle array length, can not find the triangle [I] [j], and the following is a possible, created a one dimensional array pointer, each one dimensional pointer pointing to a separate memory space
3. Aimed at the problems in question 2, similar to the situation of the subject, how to gracefully to a multidimensional array using an array subscript to operation,
Attach my code
* * triangle int minimumTotal (int, int triangleSize, int * triangleColSize) { Int cache=(int) * * * * malloc (sizeof (int *) * triangleSize); Int triangle_p=(int) * * * * malloc (sizeof (int *) * triangleSize); Int res=99999999999; If (1==triangleSize) Return triangle [0] [0]. else if (! TriangleSize) return -1; For (int I=0; I & lt; TriangleSize; I++) { Cache [I]=(int *) malloc (sizeof (int) * triangleColSize [I]); Memset (cache [I], 0, sizeof (int) * triangleColSize [I]);