题目:
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、尽量减少中间变量,保证代码可读性。

题目:
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、尽量减少中间变量,保证代码可读性。