Analysis of Leetcode Problem Solving Ideas (27) 193-199

  1. Valid phone numbers
    Given a text file file.txt containing a list of phone numbers (one phone number per line), write a bash script to output all valid phone numbers. You can assume that a valid phone number must meet the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (X represents a number) You can also assume that there are no extra space characters before and after each line.

There are three key points of regular expressions: special characters, qualified characters, and anchors

Expression (xxx) xxx-xxxx

^\([0-9][0-9][0-9]\) [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$

Use qualifiers to limit the number of times the number appears, optimized to the following expression

^\([0-9]{
    
    3}\) [0-9]{
    
    3}-[0-9]{
    
    4}$

Expression xxx-xxx-xxxx

^[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$

Use qualifiers to limit the number of times the number appears, optimized to the following expression

^[0-9]{
    
    3}-[0-9]{
    
    3}-[0-9]{
    
    4}$

In combination, use special characters () and |. Use () to mark an expression, and use | to indicate any choice between two items.

# Read from the file file.txt and output all valid phone numbers to stdout.
awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt
  1. Transpose file
    Given a file file.txt, transpose its content.
    You can assume that the number of columns in each row is the same, and each field is separated by ''.

Awk processes text files line by line. The running process is:
first run {Action} after BEGIN, which is equivalent to the header,
and then run the main command of file processing
in {Action}. Finally, how many commands
are in {Action} after running END A frequently used awk constant: NF is the number of field fields in the current row; NR is the current number of rows being processed.
Note that it is transposition. If the original text has m rows and n columns (fields), then the transposed text should have n rows and m columns, that is, each field of the original text corresponds to a row of the new text. We can use the array res to store the new text, and store each line of the new text as an element of the array res.
Before END, we traverse each line of file.txt and make a judgment: in the first line, every time a field is encountered, it is placed in the res array in order; starting from the second line, every time a field is encountered The field is appended to the end of the corresponding element (with a space in between).
The text is processed and finally needs to be output. Traverse the array after END, and output each row. Note that printf will not wrap automatically, but print will wrap automatically.

# Read from the file file.txt and print its transposed content to stdout.
awk '{
    for (i=1;i<=NF;i++){
        if (NR==1){
            res[i]=$i
        }
        else{
            res[i]=res[i]" "$i
        }
    }
}END{
    for(j=1;j<=NF;j++){
        print res[j]
    }
}' file.txt


  1. Tenth line
    Given a text file file.txt, please print only the tenth line in this file.
# Read from the file file.txt and output the tenth line to stdout.
awk 'NR == 10{print $0}' file.txt
  1. Delete duplicate emails
# Write your MySQL query statement below
DELETE p1 FROM Person p1,
    Person p2
WHERE
    p1.Email = p2.Email AND p1.Id > p2.Id



  1. Rising temperature
    Given a Weather table, write a SQL query to find the Id of all dates with a higher temperature than the previous (yesterday) date.
# Write your MySQL query statement below
SELECT a.Id
FROM (
	SELECT w.Id, w.Temperature
		, if(w.Temperature > @last_T
			AND datediff(w.RecordDate, @last_D) = 1, 1, 0) AS is_greater
		, @last_T := w.Temperature, @last_D := w.RecordDate
	FROM Weather w, (
			SELECT @last_T := 100, @last_D := 1
		) b
	ORDER BY RecordDate ASC
) a
WHERE a.is_greater = 1
ORDER BY a.Id ASC

  1. House Robbery Given an array of non-negative integers representing the amount of money stored in each house, calculate the maximum amount you can steal in one night without triggering the alarm device. If two adjacent houses are broken into by a thief on the same night, the system will automatically call the police.

This question is a simple dynamic programming problem. The state of each bit depends on whether you choose to steal the bit. If you steal, it is dp[i-2] + nums[i], otherwise it is dp[i-1]

class Solution {
    
    
public:
    int rob(vector<int>& nums) {
    
    
        int n = nums.size();
        if (n == 0)
            return 0;
        if (n == 1)
            return nums[0];

        int ret = 0, i = 2;
        vector<int> dp(nums);
        if (dp[1] < dp[0])
            dp[1] = dp[0];
        while (i < n)
        {
    
    
            dp[i] = max(dp[i - 2] + nums[i], dp[i -1]);
            i++;
        }
        return dp[n - 1];
    }
};
  1. Right view of the
    binary tree Given a binary tree, imagine yourself standing on its right side, in order from top to bottom, return the node values ​​that can be seen from the right.

This problem can be solved by DFS and BFS. In the case of DFS, the root->right subtree->left subtree is traversed in order, and if the depth has been output, it will not continue to be stored. In the case of BFS, traverse layer by layer, the last element of each layer is the right view

/**
 * Definition for a binary tree node.
 * struct TreeNode {
    
    
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {
    
    }
 * };
 */
class Solution {
    
    
    vector<int> ret;
public:
    vector<int> rightSideView(TreeNode* root) {
    
    
        dfs(root, 0);
        //bfs(root);
        return ret;
    }

    void dfs(TreeNode *node, int depth)
    {
    
    
        if (node == NULL) return;    
        if (depth == ret.size())
        {
    
    
            ret.push_back(node->val);
        }
        depth++;
        dfs(node->right, depth);
        dfs(node->left, depth);
    }
    
    void bfs(TreeNode *node)
    {
    
    
        queue<TreeNode *> line;
        if (node == NULL) return;
        line.push(node);

        while (line.size())
        {
    
    
            int size = line.size();
            for (int i = 0; i < size; i++)
            {
    
    
                TreeNode *now = line.front(); 
                line.pop();
                if (now->left) line.push(now->left);
                if (now->right) line.push(now->right);
                if (i == size - 1) ret.push_back(now->val);
            }
        }

    }
};

Guess you like

Origin blog.csdn.net/u013354486/article/details/106344479