LeetCode 2015.7.15 27,20,19,14,232,118
2016-02-13 20:30
405 查看
LeetCode 2015.7.15 27,20,19,14,232,118 27 Remove Element class Solution { public: int removeElement(vector<int>& nums, int val) { int len=nums.size(); vector<int> tmp; tmp.clear(); for(int i=0;i<len;i++) { if (nums[i]!=val) tmp.push_back(nums[i]); } nums.clear(); int lentmp=tmp.size(); nums.resize(lentmp); for(int i=0;i<lentmp;i++) nums[i]=tmp[i]; return lentmp; } }; 20 Valid Parentheses class Solution { public: bool isValid(string s) { stack<int> st; int flag[300]={0}; flag['[']=1;flag[']']=-1; flag['{']=2;flag['}']=-2; flag['(']=3;flag[')']=-3; while (!st.empty()) st.pop(); st.push(4); int len=s.size(); for(int i=0;i<len;i++) { if (flag[s[i]]>0) st.push(flag[s[i]]); else { if (flag[s[i]]+st.top()==0) st.pop(); else return false; } } if (st.size()!=1) return false;else return true; } }; 19 Remove Nth Node From End of List class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { int len=0; ListNode* tmphead=head; while (tmphead!=NULL) { len++; tmphead=tmphead->next; } int split=len-n; tmphead=head; for(int i=1;i<split;i++) { tmphead=tmphead->next; } if (n==len) head=head->next; else tmphead->next=tmphead->next->next; return head; } }; 14 Longest Common Prefix class Solution { public: string longestCommonPrefix(vector<string>& strs) { int num= strs.size(); if (num==0) return ""; int len=INT_MAX; for(int i=0;i<num;i++) { if (strs[i].size()<len) len=strs[i].size(); } if (len==0) return ""; string ans; bool flag=true; ans.clear(); for(int i=0;i<len;i++) { for(int j=1;j<num;j++) { if (strs[j][i]!=strs[j-1][i]) { flag =false; break; } } if (!flag) break; ans=ans+strs[0][i]; } return ans; } }; 232 Implement Queue using Stacks class Queue { private: stack<int> s1,s2; public: // Push element x to the back of queue. void push(int x) { s1.push(x); } // Removes the element from in front of queue. void pop(void) { if (!s2.empty()) s2.pop(); else { while (!s1.empty()) { s2.push(s1.top()); s1.pop(); } s2.pop(); } } // Get the front element. int peek(void) { if (!s2.empty()) return s2.top(); while (!s1.empty()) { s2.push(s1.top()); s1.pop(); } return s2.top(); } // Return whether the queue is empty. bool empty(void) { if (s1.empty() && s2.empty()) return true; return false; } Queue(){ while (!s1.empty()) s1.pop(); while (!s2.empty()) s2.pop(); } }; 118 Pascal's Triangle class Solution { public: vector< vector<int> > generate(int numRows) { vector< vector<int> > ans; vector<int> line; ans.clear(); if (numRows==0) return ans; line.clear(); line.push_back(1); ans.push_back(line); if (numRows==1) return ans; for(int i=1;i<numRows;i++) { line.clear(); line.push_back(1); int len=i-1; for(int j=0;j<len;j++) line.push_back(ans[i-1][j]+ans[i-1][j+1]); line.push_back(1); ans.push_back(line); } return ans; } };
相关文章推荐
- FPGA中的时序分析(三)
- 自己写的一个vii总结
- 字符串_笔记
- 关于coursera上Learning How to Learn课程的读书笔记
- 懒加载的一些理解
- Java中泛型中的几个符号
- LeetCode 2015.7.13 9,21,104,6,191,198,204,225
- bzoj 4016 [FJOI2014]最短路径树问题(最短路径树+树分治)
- iOS 获取项目文件路径
- 杭电1059Windows Message Queue
- LeetCode 2015.7.10 26,58,226
- 一致性Hash算法
- LeetCode 2015.7.9 66,67,70,83,88,100
- 算法精讲学习笔记 队列和栈
- github 和 jupyter
- FPGA中的时序分析(二)
- 在MFC类中各种类的指针的获取和应用
- “红包”
- bestcoder#71
- view文档的分栏