第十周ARTS

A

Leetcode394——Decode String

题目要求

Given an encoded string, return it’s decoded string.

The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.

You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.

Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won’t be input like 3a or 2[4].

s = "3[a]2[bc]", return "aaabcbc".

s = "3[a2[c]]", return "accaccacc".

s = "2[abc]3[cd]ef", return "abcabccdcdcdef".

解题代码:

class Solution {

public:

    string decodeString(string s) {

        string m = "";      //用于记录字符串

        stack s_num;          //创建一个栈来保存遍历到的数字

        stack s_str;      //创建另一个栈来保存字符串

        int sn = 0;      //用于记录数字

        for (int i = 0; i < s.size(); ++i)

        {

            if (s[i] >= '0' && s[i] <= '9')

            {

                sn=10 *sn+s[i]-'0';  //得到s中的数字

            }

            else if (s[i] == '[') 

            /*遍历到左括号,则把数字sn压入栈中,

            *同时清空sn,用于记录下一个数字

            *同时也把之前记录的字符串压入栈中

            *用于之后的字符串相加操作

            */

            {

                s_num.push(sn);

                s_str.push(m);

                sn=0;

                m.clear();

            }

            else if (s[i] == ']')

            /*遍历到右括号,则把栈中顶层数字取出来

            *开始进行解码操作

            */

            {

                int k = s_num.top();

                s_num.pop();

                for (int j = 0; j < k; ++j)

                {

                    s_str.top() += m;

                }

                m = s_str.top();

                s_str.pop();

            }

            else

            /*记录字符串*/

            {

                m += s[i];

            }

        }

        return m;

    }

};



R

关于如何处理不平衡数据集

https://mp.weixin.qq.com/s/n__cQQzdage_CYROYR5oZg


T

二叉树中两个节点之间的路径,从查找最近公共父节点衍生出来的。

/**

*  二叉树中两个节点之间的路径

*

*  @param nodeA    第一个节点

*  @param nodeB    第二个节点

*  @param rootNode 二叉树根节点

*

*  @return 两个节点间的路径

*/

+ (NSArray *)pathFromNode:(BinaryTreeNode *)nodeA toNode:(BinaryTreeNode *)nodeB inTree:(BinaryTreeNode *)rootNode {

    if (!rootNode || !nodeA || !nodeB) {

        return nil;

    }

    NSMutableArray *path = [NSMutableArray array];

    if (nodeA == nodeB) {

        [path addObject:nodeA];

        [path addObject:nodeB];

        return path;

    }

    //从根节点到节点A的路径

    NSArray *pathA = [self pathOfTreeNode:nodeA inTree:rootNode];

    //从根节点到节点B的路径

    NSArray *pathB = [self pathOfTreeNode:nodeB inTree:rootNode];

    //其中一个节点不在树中,则没有路径

    if (pathA.count == 0 || pathB == 0) {

        return nil;

    }

    //从后往前推,查找第一个出现的公共节点

    for (NSInteger i = pathA.count-1; i>=0; i--) {

        [path addObject:[pathA objectAtIndex:i]];

        for (NSInteger j = pathB.count - 1; j>=0; j--) {

            //找到公共父节点,则将pathB中后面的节点压入path

            if ([pathA objectAtIndex:i] == [pathB objectAtIndex:j]) {

                j++; //j++是为了避开公共父节点

                while (j

                    [path addObject:[pathB objectAtIndex:j]];

                    j++;

                }


                return path;

            }

        }

    }

    return nil;

}


S

网络爬虫抓取礼仪

http://lmgtfy.com/?q=web+scraping+etiquett


你可能感兴趣的:(第十周ARTS)