Task01: Array

Theoretical part

  • Understanding storage and classification of the array.
  • Dynamic array, which can be modified as needed length of the array.

Practice section

1. The use of dynamic arrays to solve data storage problems

To write a piece of code, a required input integer N, with dynamic array Ato store [ 2,N]multiples between all 5 or 7, the output of the array.

Example:

Input: 
N = 100 

Output: 
. 5 7,101,415,202,125,283,035 40 4,245,495,055,566,063 65 70 75 7,780,848,590,919,598,100

solution:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>


int n,*array = 0,j=0;
void Input()
{
    int i = 0,num = 0;
    scanf("%d",&n);
   
    array = (int *)malloc(sizeof(int)*n);
    
    if(array == 0)
    {
        printf("error");
    }

  
    for(i=2;i<=n;i++)
    {
        if(i%5==0||i%7==0)
        {
            array[j]=i;
            j++;
        }
    }
}

void Outpu(){
    for(int i=0;i<j;i++)
        printf("%d ",array[i]);
}

int main()
{
    Input();
    Outpu();
    free(array);
    return 0;
}

2. Toeplitz matrix problem

If each direction of a matrix having the same elements from the upper left to lower right diagonal, then the matrix is ​​a Toeplitz matrix.

Given a M x Nmatrix, which is returned only when and if Toeplitz matrix True.

Example:

Input: 
Matrix = [ 
  [1,2,3,4], 
  [5,1,2,3], 
  [9,5,1,2] 
] 

Output: True

Explanation:

In the above matrix, which diagonal:  "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]". All elements on each diagonal lines are the same, so the answer is True.

 

solution:

matrix = [ [1,2,3,4], [5,1,2,3], [9,5,1,2] ]

def solve(matrix):
    for r in range(len(matrix)-1):
        for c in range(len(matrix[0])-1):
            if matrix[r+1][c+1] and matrix[r][c] != matrix[r+1][c+1]:
                return False
    return True

solve(matrix)

3. sum Noriyuki three number

https://leetcode-cn.com/problems/3sum/

Given an array of n comprises integers nums, it determines numswhether there are three elements a,b,c, such that a + b + c = 0? All the conditions are not satisfied to find duplicate triples.

Note: The answer can not contain duplicate triples.

Example:

Given array nums = [-1, 0, 1 , 2, -1, -4], 

meet the requirements of the set of triples as: 
[ 
  [-1, 0, 1], 
  [-1, -1, 2] 
]

solution:

public class SumThree {
	public static void main(String[] args) {
		int[] nums={-1, 0, 1, 2, -1, -4};
		List<List<Integer>> ans = new ArrayList<>();
        if(nums == null || nums.length < 3){
        	System.out.println(ans);
        }
        Arrays.sort(nums); // 排序
        for (int i = 0; i < nums.length ; i++) {
            if(nums[i] > 0) break;
            if(i > 0 && nums[i] == nums[i-1]) continue; 
            int L = i+1;
            int R = nums.length-1;
            while(L < R){
                int sum = nums[i] + nums[L] + nums[R];
                if(sum == 0){
                    ans.add(Arrays.asList(nums[i],nums[L],nums[R]));
                    while (L<R && nums[L] == nums[L+1]) L++; 
                    while (L<R && nums[R] == nums[R-1]) R--; 
                    L++;
                    R--;
                }
                else if (sum < 0) L++;
                else if (sum > 0) R--;
            }
        }        
        System.out.println(ans);
	}

}

 

Released eight original articles · won praise 1 · views 196

Guess you like

Origin blog.csdn.net/Moby97/article/details/103866763