leetcode随笔VII

  1. leetcode题目
    1.1题目描述
    1.2知识点及思路
    1.3代码
  2. 总结

一.leetcode题目
1.Isomorphic Strings
题目描述:判断字符串是同构
Given “egg”, “add”, return true.
Given “foo”, “bar”, return false.
Given “paper”, “title”, return true.
知识点:hashTable
思路:①以字符做下标建立字符表(用于表征存在性)②排除字母顺序,按照0,1…..进行编号③判断数字表是否相同 举例:cbc 表示为“121”aba表示为“121”,两者同构
代码如下:

class Solution {
private:
    string trans(string&s)
    {
        char table[128]={0};
        char temp='1';
        for(int i=0;i<s.size();i++)
        {
            if(table[s[i]]==0)
            {
                table[s[i]]=temp;
                temp++;
            }
            s[i]=table[s[i]];
        }
        return s;
    }
public:
    bool isIsomorphic(string s, string t) 
    {
        if(trans(s)==trans(t))
            return true;
        return false;
    }
};

2.Word Pattern
题目描述:判断是否同模型
Examples:
pattern = “abba”, str = “dog cat cat dog” should return true.
pattern = “abba”, str = “dog cat cat fish” should return false.
pattern = “aaaa”, str = “dog cat cat dog” should return false.
pattern = “abba”, str = “dog dog dog dog” should return false.
知识点:hashTable;字符切分
思路:①按照题目1思路,建立同构数组②切分str字符串③循环判断是否符合同构条件
代码如下:

class Solution {
private:
    string trans(string&s)
    {
        char table[128]={0};
        char temp='1';
        for(int i=0;i<s.size();i++)
        {
            if(table[s[i]]==0)
            {
                table[s[i]]=temp;
                temp++;
            }
            s[i]=table[s[i]];
        }
        return s;
    }
   void split(const string& src, const string& separator, vector<string>& dest)
{
    string str = src;
    string substring;
    string::size_type start = 0, index;

    do
    {
        index = str.find_first_of(separator,start);
        if (index != string::npos)
        {    
            substring = str.substr(start,index-start);
            dest.push_back(substring);
            start = str.find_first_not_of(separator,index);
            if (start == string::npos) return;
        }
    }while(index != string::npos);

    //the last token
    substring = str.substr(start);
    dest.push_back(substring);
} 
public:
    bool wordPattern(string pattern, string str) 
    {
        vector<string>dest;
        split(str," ",dest);
        if(pattern.size()!=dest.size())
            return false;
        for(int i=0;i<pattern.size()-1;i++)
            for(int j=i+1;j<pattern.size();j++)
            {
                if(pattern[i]==pattern[j])
                    if(dest[i]!=dest[j])
                     return false;
                if(pattern[i]!=pattern[j])
                    if(dest[i]==dest[j])
                        return false;
            }
        return true;       
    }
};

3.Valid Parentheses
题目描述:判断括号(’()’,’[]’,’{}’)匹配是否符合要求
知识点:栈;hashTable
思路:①括号建hashTable映射②将字符串中键入栈③字符串中检测到值则出栈
代码如下:

class Solution {
public:
    bool isValid(string s) 
    {
        stack<char>sto;
        map<char,char> biaoHao;
        biaoHao.insert(make_pair('(',')'));
        biaoHao.insert(make_pair('[',']'));
        biaoHao.insert(make_pair('{','}'));
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='{'||s[i]=='['||s[i]=='(')
                sto.push(s[i]);
            else
            {
                if(sto.empty())
                    return false;
                if(s[i]==biaoHao.find(sto.top())->second)
                    sto.pop();
                else
                    return false;
            }
        }
        if(sto.empty())
            return true;
        else
            return false;
    }
};

4.Remove Nth Node From End of List
题目描述:
Given linked list: 1->2->3->4->5, and n = 2.
the linked list becomes 1->2->3->5.
知识点:链表(无头结点)删除;链表双指针
思路:①设置两个指针②第一个指针先走k步③两个指针同时遍历,找到需删除结点④结点删除
注意事项:删除结点为头结点;删除结点为最后一个结点
代码如下:

/**
 1. Definition for singly-linked list.
 2. struct ListNode {
 3.     int val;
 4.     ListNode *next;
 5.     ListNode(int x) : val(x), next(NULL) {}
 6. };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) 
    {
        ListNode*first=head;
        ListNode*second=head;
        int temp=1;
        while(temp!=n)
        {
            first=first->next;
            temp++;
        }
        while(first->next)
        {
            first=first->next;
            second=second->next;
        }
        if(second==head)
        {
            head=head->next;
            second->next=NULL;
        }
        else
        {
            if(!second->next)
            {
                ListNode*temp=head;
                while(temp->next!=second)
                    temp=temp->next;
                temp->next=NULL;
            }
            else
            {
                second->val=second->next->val;
                second->next=second->next->next;
            }
        }
        return head;    
    }
};

5.Length of Last Word
题目描述:返回最后一个字符串长度
For example,
Given s = “Hello World”,
return 5.
知识点:字符切分
思路:①设置开始位置下标0,找到字符切分下标②取子串③更新起始位置下标④循环①②③步骤
代码如下:

class Solution {
private:
void split(const string& src, const string& separator, vector<string>& dest)
{
    string str = src;
    string substring;
    string::size_type start = 0, index;

    do
    {
        index = str.find_first_of(separator,start);
        if (index != string::npos)
        {    
            substring = str.substr(start,index-start);
            dest.push_back(substring);
            start = str.find_first_not_of(separator,index);
            if (start == string::npos) return;
        }
    }while(index != string::npos);

    //the last token
    substring = str.substr(start);
    dest.push_back(substring);
}
public:
    int lengthOfLastWord(string s) 
    {
        vector<string>dest;
        split(s," ",dest);
        if(dest.size()==0)
            return 0;
        return dest[dest.size()-1].size();
    }
};

二.总结
I.题目的构思来源于生活,平时积累亦很重要II.让我们一同努力,明天会更好!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值