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:
- 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.
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;
}
};
注:自己新建头结点!
- leetcode学习笔记5
- LeetCode学习笔记
- leetcode学习笔记1
- leetcode学习笔记2
- leetcode学习笔记3
- leetcode学习笔记4
- Leetcode学习笔记
- leetcode学习笔记
- LeetCode学习笔记-Day1
- LeetCode学习笔记-Day2
- leetcode学习笔记:Combinations & Subsets
- 【leetcode学习笔记】leecode是什么?
- leetcode学习笔记:Trapping Rain Water
- LeetCode 学习笔记:Question 1、Two Sum
- 算法学习笔记[LeetCode]Valid Parentheses
- 算法学习笔记[LeetCode]Min Stack
- 算法学习笔记[LeetCode][ZigZag Conversion]
- LeetCode学习笔记[1]:Two Sum
- 算法_2:数值算法
- HDOJ 5606-tree【并查集】
- 【转载】光流法简单介绍
- 运行MIT-GNU Scheme出现“Requested allocation is too large”
- 总结一下自己的2015
- leetcode学习笔记5
- 自定义等高的cell(代码创建frame)
- Fill the Square
- 【Java设计模式】详解系列 3.1.2行为型模式 : 观察者模式 — 模式详解
- shell 中的continue
- HDU 1005.Number Sequence【用递归会超时】(2.5)
- eval解析JSON注意点
- Theano 逻辑回归教程 的注释
- HDU 1247 Hat’s Words