Leetcode(105)

这个题我看到网上有一个很优美的解法,是利用static variable来做的,因为preorder的第一个元素一定是子树的根,同时,下一个元素一定是左子树的根。以此类推。接下来我们在inorder中寻找这个元素对应的位置,那么从start到这个点是左子树,从这个点到end是右子树。这是一个递归的方法。

这个做法非常好,唯一而且是致命的缺点是static变量只能初始化一次,所以这个函数只能运行一次。很尴尬。正确答案如下:

TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
    return create(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);
}

TreeNode* create(vector<int>& preorder, vector<int>& inorder, int ps, int pe, int is, int ie){
    if(ps > pe){
        return nullptr;
    }
    TreeNode* node = new TreeNode(preorder[ps]);
    int pos;
    for(int i = is; i <= ie; i++){
        if(inorder[i] == node->val){
            pos = i;
            break;
        }
    }
    node->left = create(preorder, inorder, ps + 1, ps + pos - is, is, pos - 1);
    node->right = create(preorder, inorder, pe - ie + pos + 1, pe, pos + 1, ie);
    return node;
}

Let me explain the coordinates in the recursion. Very simply, we can see that the inorder traversal is divided into two parts, [is, pos-1] and [pos+1, ie] according to the root node pointed by pos.The first part contains pos – is elements, and the second part has ie- (pos +1)+1 = ie – pos elements.
Correspondingly, in preorder traversal, the elements in the [ps+1, ps+pos – is] intervals belong to the left subtree, and the elements in the [pe – (ie – pos)+1, pe] interval belong to the right subtree.

Leetcode(83)

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* curr=head;
        while(curr && curr->next){
            if(curr->val==curr->next->val )
                curr->next=curr->next->next;
            else{
                curr=curr->next;
            }
            
        }
        return head;
    }
};

这个题目的答案不用多讲,答案不言自明。但是我在做这个题目的时候犯了一个错误,curr=curr->next这个不是每次都要更新的。只有当找不到重复的时候我们才再去更新一次。这个其实你也想到过,你当时定义了一个dhead指针,但很复杂,不简洁。

Leetcode(2)

这个题,简而言之就是做一个加法,但是加法的数字不是存储为int格式,而是以linked list的形式储存。同时它返回也要求是一个linked list.所以这个题有两个难点,一是设计出加法计算的算法,二是如何使用构造函数构造出一个新的linked list.关于列表的构造,其实我一直不是很熟练,主要是这个结构有些抽象,而且这个涉及类的构造。说实话我一直很晕。

下面我们先看看答案:

Definition for singly-linked list.

struct ListNode {

int val;

ListNode *next;

ListNode(int x) : val(x), next(NULL) {}

};
/ class Solution { public: ListNode addTwoNumbers(ListNode* l1, ListNode* l2) {
if (l1 == NULL and l2 == NULL) return NULL;
else if (l1 == NULL) return l2;
else if (l2 == NULL) return l1;

int a = l1->val + l2->val;

ListNode *p = new ListNode(a % 10);

p->next = addTwoNumbers(l1->next,l2->next);

if (a >= 10) p->next = addTwoNumbers(p->next, new ListNode(1));

return p; }

1.题目给的节点类的设计要记住,后面会围绕这个进行构造。

2.本题使用的是递归的形式,所以要将基类情况进行说明。

3. 构造节点有两种形式 Node* p = new Node(val),这里使用的是指针的构造,p是指向Node结构的指针。Node p= Node(val)。但是我们要使用->,所以我们选择了第一种。

4.最后一行是因为如果超过了10 要加一

R-Squared vs Correlation

1.R-Squared:

The coefficient of determination, denoted R2 or r2 and pronounced “R squared”, is the proportion of the variance in the dependent variable that is predictable from the independent variable(s).

It is a stastical used in the context of statistical models whose main purpose is either the prediction of future outcomes or the testing of  hypothetes, on the basis of other related information. It provides a measure of how well observed outcomes are replicated by the model, based on the proportion of total variation of outcomes explained by the model.

There are several definitions of R2 that are only sometimes equivalent. One class of such cases includes that of simple linear regression where r2 is used instead of R2. When an intercept is included, then r2 is simply the square of the sample correlation coefficient (i.e., r) between the observed outcomes and the observed predictor values. If additional regressors are included, R2 is the square of the coefficient of multiple correlation. In both such cases, the coefficient of determination normally ranges from 0 to 1.

The most general definition of the coefficient of determination is: SST = SSR+SSE

{\displaystyle R^{2}\equiv 1-{SS_{\rm {res}} \over SS_{\rm {tot}}}\,}

2.Correlation

Pearson’s correlation coefficient is the  covariance of the two variables divided by the product of their standard deviations. The form of the definition involves a “product moment”, that is, the mean (the first moment about the origin) of the product of the mean-adjusted random variables; hence the modifier product-moment in the name.

Pearson’s correlation coefficient when applied to a population is commonly represented by the Greek letter ρ (rho) and may be referred to as the population correlation coefficient or the population Pearson correlation coefficient. Given a pair of random variables {\displaystyle (X,Y)}, the formula for ρ[7] is:

{\rho _{X,Y}={\frac {{cov} (X,Y)}{\sigma_{X}\sigma _{Y}}}}    (Eq.1)

where: