leetcode学习笔记5

来源:互联网 发布:sqlserver history 编辑:程序博客网 时间:2024/05/16 19:37

205. Isomorphic Strings

Given two strings s and t, determine ifthey are isomorphic.

 

Two strings are isomorphic if thecharacters in s can be replaced to get t.

 

All occurrences of a character must bereplaced with another character while preserving the order of characters. Notwo characters may map to the same character but a character may map to itself.

 

For example,

Given "egg", "add",return true.

 

Given "foo", "bar",return false.

 

Given "paper", "title",return true.

class Solution {

public:

   bool isIsomorphic(string s, string t) {

      int a[256]={0};

      int b[256]={0};

      int i;

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

          if(a[s[i]]&&a[s[i]]==b[t[i]])

          continue;

          if(!a[s[i]]&&!b[t[i]]){

               a[s[i]]=i+1;

               b[t[i]]=i+1;

              continue;

          }

          return false;

      }

      return true;

    }

};

:算法思想是利用桶排序的原理,判断字母出现次数。

20. Valid Parentheses

Given a string containingjust the characters '('')''{''}''[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

Subscribe to see which companies asked thisquestion

class Solution {

public:

   bool isValid(string s) {

       map<char,char> mp;

       mp[')']='(';

       mp[']']='[';

       mp['}']='{';

       stack<char> stack;

       for(auto c:s){

           if(mp.find(c)!=mp.end()&&!stack.empty())

                if(mp[c]==stack.top())

                stack.pop();

                else return false;

           else stack.push(c);

       }

       return stack.empty();

}

};

注:使用关联容器进行匹配,从而进出栈

290. Word Pattern

Given a pattern and astring str, find if str follows the same pattern.

Here follow meansa full match, such that there is a bijection between a letter in pattern and a non-empty wordin str.

Examples:

  1. pattern = "abba", str = "dog cat cat dog" should return true.
  2. pattern = "abba", str = "dog cat cat fish" should return false.
  3. pattern = "aaaa", str = "dog cat cat dog" should return false.
  4. pattern = "abba", str = "dog dog dog dog" should return false.

class Solution {

public:

   bool wordPattern(string pattern, string str) {

       unordered_map<char,string> mp1;

       unordered_map<string,char> mp2;

       int len=pattern.size(),n=str.size();

       int i=0,j=0;

       string word="";

       while(i<len){

           if(j>n) return false;

           while(str[j]!=' '&&str[j]!='\0')

           word=word+str[j++];

           if(mp1.find(pattern[i])!=mp1.end()&&mp1[pattern[i]]!=word)

           return false;

           if(mp2.find(word)!=mp2.end()&&mp2[word]!=pattern[i])

           return false;

           mp1[pattern[i]]=word;

           mp2[word]=pattern[i];

           i++;j++;

           word="";

       }

       if(j<n) return false;

       return true;

       

    }

};

注:关联容器!!

38. Count and Say

Thecount-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211,111221, ...

1 is readoff as "one 1" or 11.
11 is readoff as "two 1s" or 21.
21 is readoff as "one 2, then one 1" or 1211.

class Solution {

public:

   string countAndSay(int n) {

      

   string res = "1", tmp;

   if(n==1) return res;

   int count, i, j;

   for(i = 2; i<=n; i++){

       tmp = res;

       res.clear(); count = 1;

       for(j = 1; j<tmp.size(); j++){

           if(tmp[j-1]==tmp[j]) count++;

           else{

                res.push_back('0'+count);

                res.push_back(tmp[j-1]);

                count = 1;

           }

       }

       res.push_back('0'+count);

        res.push_back(tmp[j-1]);  这里注意!!!!

    }

   return res;

       

    }

};

203. Remove Linked List Elements

Remove allelements from a linked list of integers that have value val.

Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 -->6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5

class Solution {

public:

   ListNode* removeElements(ListNode* head, int val) {

       if(!head) return NULL;

       ListNode* realhead=new ListNode(0);

       realhead->next=head;

       ListNode* pre=realhead;

       ListNode* p;

       p=head;

       while(p){

           if(p->val==val){

           pre->next=p->next;

           p=p->next;}

           else {p=p->next;

           pre=pre->next;}

       }

       return realhead->next;

 

       

       

    }

};

注:自己新建头结点!

0 0
原创粉丝点击