list array simple exercise 2

Keywords: Algorithm leetcode list

1. Convert an ordered array into a binary search tree

Give you an integer array nums, in which the elements have been arranged in ascending order. Please convert it into a highly balanced binary search tree.

A height balanced binary tree is a binary tree that satisfies the requirement that the absolute value of the height difference between the left and right subtrees of each node does not exceed 1.

Source: LeetCode
Link: https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Algorithmic thought

The left and right subtrees are divided equally, and the middle node is used as the root node of the subtree to recurse the process
Concrete implementation

class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
 return  helper(nums,0,nums.length-1);
    }

    public TreeNode helper(int[] nums, int left, int right){

        if(left>right){
            return  null;
        }

        int mid=(left+right)/2;
        TreeNode root=new TreeNode(nums[mid]);
        root.left=helper(nums,left,mid-1);
        root.right=helper(nums,mid+1,right);
        
        return root;
    }
    
}

2. Yanghui triangle

Given a nonnegative integer numRows, the first numRows of the Yang Hui triangle are generated.

In the Yang Hui triangle, each number is the sum of its upper left and upper right numbers.

Algorithmic thought

1. Create an array. When i == j or the first column, i.e. j = 0, the value is 1,
2 the rest is that the current value is equal to its upper left value and upper value, that is, num[i - 1][j - 1] + num[i - 1][j];
Concrete implementation

public class yangHuiOne {
    public List<List<Integer>> generate(int numRows) {

        List<List<Integer>> list=new ArrayList<>();

        int[][] array=new int[numRows][numRows];
        for(int i=0;i<numRows;i++){
            List<Integer> subArray=new ArrayList<>();
            for(int j=0;j<=i; j++){
                if(j==0||j==i){
                    array[i][j]=1;
                }else {
                    array[i][j]=array[i-1][j-1]+array[i-1][j];
                }
                subArray.add(array[i][j]);
            }
            list.add(subArray);
        }
        return list;
    }
}

3. Yanghui triangle 2

Given a nonnegative index rowIndex, return the rowIndex row of "Yang Hui triangle".

In the Yang Hui triangle, each number is the sum of its upper left and upper right numbers.

Algorithmic thought
Concrete implementation

class Solution {
    public List<Integer> getRow(int rowIndex) {

   List<Integer> list = new ArrayList<>();
        if (rowIndex == 0) {
            list.add(1);
        }
        if (rowIndex == 1) {
            list.add(1);
            list.add(1);
        }
        if (rowIndex > 1) {
            List<Integer> list1 = getRow(rowIndex - 1);
            int n = list1.size();
            list.add(1);
            for (int i = 1; i < n; i++) {
                int num = list1.get(i - 1) + list1.get(i);
                list.add(num);
            }
            list.add(1);
        }

        return list;
        }
}

4. The best time to buy stocks

Given an array of prices, its ith element prices[i] represents the price of a given stock on day I.

You can only choose to buy this stock one day and sell it on a different day in the future. Design an algorithm to calculate the maximum profit you can make.

Return the maximum profit you can make from this transaction. If you can't make any profit, return 0.

Source: LeetCode
Link: https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Algorithmic thought
Maximum income in the first i days of dynamic programming = max {maximum income in the first i-1 days, price in the first i-1 days - minimum price in the first i-1 days}
Concrete implementation

class Solution {
    public int maxProfit(int[] prices) {

     if(prices==null||prices.length<=1){
            return 0;
        }
        int min=prices[0],max=0;
        for(int i=1;i<prices.length;i++){
            max = Math.max(max, prices[i] - min);
            min = Math.min(min, prices[i]);
        }
        
        return max;
    }
}

5. Figures that appear only once

Given a non empty array of integers, each element appears twice except that one element appears only once. Find the element that appears only once.

explain:

Your algorithm should have linear time complexity. Can you do it without using extra space?

Source: LeetCode
Link: https://leetcode-cn.com/problems/single-number
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Algorithmic thought
Use XOR
Concrete implementation

class Solution {
    public int singleNumber(int[] nums) {
         if(nums.length==1){
            return nums[0];
        }

        for(int i=1;i<nums.length;i++){
            nums[0] ^=nums[i];
        }

        return nums[0];
    }
}

Posted by hanhao on Sun, 19 Sep 2021 19:49:49 -0700