题目:
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

思路:
该题目是求二叉树的最大深度,分析每个节点的规律可发现明显的规律,每个节点都有左右之分,依次去求左右节点的深度即可求其最大深度。使用递归法求解。复杂度为:O(n)

代码:

/**

  • 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 {
public:

int maxDepth(TreeNode* root) {
    if( root == NULL )
    {
        return 0;
    }

    int leftDepth = maxDepth(root->left);
    int rightDepth = maxDepth(root->right);
     
    return max(leftDepth, rightDepth) + 1;
}

};
总结:
1、对于有规则的循环嵌套可以使用迭代法
2、递归法要有一个终结点,找到适当的终结点即可。如该题是 root == NULL 终结掉

题目:
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

思路:
建立一个map去存储已经循环过的数字,每次循环前先判断该值是否存在于map中,如果map内没有则添加进去,如果存在则清空该记录,最后循环完后,map里只有一条记录为最终该结果。算法复杂度:O(n)

代码:

class Solution {
public:

int singleNumber(vector<int>& nums) {
    map<int, int> vaule_map;
    for( unsigned int i = 0; i < nums.size(); i++ )
    {
        map<int, int>::iterator iIter = vaule_map.find(nums[i]);
        if( iIter == vaule_map.end() )
        {
            //empty
            vaule_map[nums[i]] = 1;
        }
        else
        {
            //find it and erase it
            vaule_map.erase(iIter);
        }
    }
    if( vaule_map.size() != 1 )
    {
        return -1;
    }
     
    map<int, int>::iterator iBegin = vaule_map.begin();
     
    return iBegin->first;
}

};
总结:
1、在网上看到其他解题思路,虽然可以节省内存,但着实没看下去。。
2、第一次提交一次通过!好开心。

题目:
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

思路:
刚开始没怎么看懂题目,明明是删除一个节点需要的输入参数是链表和节点,但题目给出参数只有节点,后来查了资料,才明白有一种思路叫做“狸猫换太子”,输入一个节点,把这个节点的下一个值赋值到这个节点,并删除下一个节点就OK了,而不是找到上一个节点的指向。算法复杂度:O(1)

代码:

/**

  • Definition for singly-linked list.
  • struct ListNode {
  • int val;
  • ListNode *next;
  • ListNode(int x) : val(x), next(NULL) {}
  • };
    */

class Solution {
public:

void deleteNode(ListNode* node) {
    if( node == NULL || node->next == NULL )
    {
        return;
    }
     
    ListNode *pNextNode = node->next;
    node->val = pNextNode->val;
    node->next = pNextNode->next;
    delete pNextNode;
}

};

赛事
2015年9月20日 北京马拉松
2015年9月13日 天津马拉松(北马意料之中的没有中签,很遗憾,首马不是在北京。)

训练周期
2015年7月20日~2015年9月20日,两个月

目标
时间:4:30
配速:6:00

配速训练
高速跑:4:30
中速跑:5:30
低速跑:6:30
配速跑:最终6:00

训练次数
每周六次,一三五力量,二四六跑步,周日休息。

训练量
一次高速跑3~5公里
一次中速的8~15公里
一次低速的LSD(15K、20K、25K、30K、35K、25K、20K,15K)最后两周减量
力量训练:平板撑、靠墙站、俯卧撑、仰卧起坐、拱桥、分腿半蹲

其他训练(2015年8月7日修改)
周二、周四 跳绳3000
周三、周五 囚徒三艺,俯卧撑(上肢)、深蹲(下肢)、拱桥(腰部)
周日 LSD 20 公里

速度
距离越短,速度越快
最后训练LSD的配速要达到 6:00

题目:
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

思路:
使用map来保存每个字符出现的位置,key:字符,value:最后一次出现的位置,遍历一次后,保存该字符到map中,并同时保存一个max结果和当前计数的索引index,index取值是取最后一次重复任何一个字符出现的位置,max(取当前索引-最后一次出现位置)的最大值。算法复杂度:O(n)!!

代码:

class Solution {
public:

int lengthOfLongestSubstring(string s)
{
    int max_result = 0;
    map<char, int> vaule_map;
    int index = -1;
    for (unsigned int i = 0; i < s.size(); i++)
    {
        map<char, int>::iterator iItr = vaule_map.find(s[i]);
        if (iItr != vaule_map.end())
        {
            index = iItr->second > index ? iItr->second : index;
        }
         
        max_result = (i - index) > max_result ? (i - index) : max_result;
         
        vaule_map[s[i]] = i;
    }
 
    return max_result;
}

};
总结:
1、当你感觉一个算法很够复杂的时候,说明这个算法还不够顺通,真正优秀的算法是很简洁且高效的。
2、尽量减少中间变量,保证代码可读性。