leetcode-string
文章平均质量分 70
varcom
这个作者很懒,什么都没留下…
展开
-
6. ZigZag Conversion
class Solution {public: string convert(string s, int numRows) { vector> vec(numRows); if(numRows==1) return s; int i,j; int n=s.size(); int tag原创 2016-09-08 14:47:34 · 133 阅读 · 0 评论 -
126. Word Ladder II
BFS+DFSclass Solution {public: map> buildTree( string beginWord, string endWord,原创 2016-09-15 15:34:22 · 445 阅读 · 0 评论 -
115. Distinct Subsequences
dpint dp[2][100000];class Solution {public: int numDistinct(string s, string t) { int n=s.size(),m=t.size(); int i,j; memset(dp,0,sizeof(dp)); if(m>n)原创 2016-09-15 15:09:37 · 178 阅读 · 0 评论 -
97. Interleaving String
动态规划bool dp[2][1000][1000];class Solution {public: bool isInterleave(string s1, string s2, string s3) { int n=s1.size(),m=s2.size(),l=s3.size(); int i,j,k; if(n+m!=l)原创 2016-09-15 13:19:01 · 200 阅读 · 0 评论 -
91. Decode Ways
没有前导0的情况,如果有连续两个0,则没有组合int dp[100000];class Solution {public: int numDecodings(string s) { int n=s.size(); if(n==0||s[0]=='0') return 0; dp[1]=1; dp原创 2016-09-15 12:47:21 · 308 阅读 · 0 评论 -
87. Scramble String
字符串分解成两颗子树,子树的长度任意,所有可以用递归class Solution {public: bool isScramble(string s1, string s2) { int n=s1.size(); int m=s2.size(); int i; if(n!=m) retu原创 2016-09-15 11:42:26 · 303 阅读 · 0 评论 -
93. Restore IP Addresses
递归class Solution {public: void valid(string s,unordered_set& adds,string add,int cnt,int len) { if(cnt>4) return; if(cnt==4) { if(s!="")原创 2016-09-15 10:38:51 · 187 阅读 · 0 评论 -
125. Valid Palindrome
双指针class Solution {public: bool isPalindrome(string s) { int n=s.size(); if(n==0) return true; int i=0,j=n-1; while(i<j) { while(原创 2016-09-15 10:33:32 · 166 阅读 · 0 评论 -
151. Reverse Words in a String
方法1:原地翻转,再单词翻转class Solution {public: void reverseWords(string &s) { vector str; int n=s.size(); int i=0,j=n-1; while(i<j) { swap(s[i],s[j]);原创 2016-09-15 09:44:42 · 246 阅读 · 0 评论 -
165. Compare Version Numbers
可能字符串中没有‘.',或者有多个‘.',所以用递归的方法更加好class Solution {public: int compareVersion(string version1, string version2) { if(version1 == version2) return 0; int po原创 2016-09-20 11:01:33 · 221 阅读 · 0 评论 -
344. Reverse String
class Solution {public: string reverseString(string s) { int n=s.size(); int i=0,j=n-1; while(i<j) { swap(s[i],s[j]); i++; j--;原创 2016-09-20 11:04:44 · 192 阅读 · 0 评论 -
345. Reverse Vowels of a String
class Solution {public: bool isV(char c) { return c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U'; } string reverseVowels(string s) { in原创 2016-09-20 11:05:18 · 186 阅读 · 0 评论 -
5. Longest Palindromic Substring
一开始n^2的写法,超时class Solution {public: void init(string s,int& maxn,int &sta) { int n=s.size(); vector> dp(n,vector(n)); int i,j; for(i=n-1;i>=0;i--) {原创 2016-09-07 23:08:48 · 190 阅读 · 0 评论 -
385. Mini Parser
/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * class NestedInteger { * public: * // Construc原创 2016-09-20 20:59:59 · 196 阅读 · 0 评论 -
383. Ransom Note
class Solution {public: bool canConstruct(string ransomNote, string magazine) { int alp[256]={0}; for(int i=0;magazine[i]!='\0';i++) alp[magazine[i]]++; for(in原创 2016-09-20 20:56:31 · 200 阅读 · 0 评论 -
336. Palindrome Pairs
class Solution {public: bool isPalin(string tmp) { int n=tmp.size(); int i=0,j=n-1; while(i<j) { if(tmp[i]!=tmp[j]) return false;原创 2016-09-20 20:53:11 · 230 阅读 · 0 评论 -
214. Shortest Palindrome
manacher 算法,寻找s[0..k]为回文串的最长回文串class Solution {public: string shortestPalindrome(string s) { string tmp="$#"; int n=s.size(); int i; for(i=0;i<n;i++) {原创 2016-09-20 13:13:02 · 301 阅读 · 0 评论 -
227. Basic Calculator II
将数据和符号分别压栈class Solution {public: int calculate(string s) { stack op; stack num; int n=s.size(); int i,j; for(i=0;i<n;i++) { if(s[i]>原创 2016-09-20 13:08:33 · 197 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
当前字母与前一个字母为结尾最长序列比较,复杂度O(n*n)class Solution {public: int lengthOfLongestSubstring(string s) { int n=s.size(); if(n==0) return 0; int i,j; int dp[n];原创 2016-09-07 16:43:55 · 262 阅读 · 0 评论 -
273. Integer to English Words
string T[10]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};string H[10]={"","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};string K[10]={"Ten"原创 2016-09-20 11:06:46 · 186 阅读 · 0 评论 -
68. Text Justification
1.最后一行的处理与前面不一样2.空格需要均匀分布class Solution {public: vector fullJustify(vector& words, int maxWidth) { int n=words.size(); vector ans; int i,j; int cur=0;原创 2016-09-14 11:50:34 · 206 阅读 · 0 评论 -
76. Minimum Window Substring
使用双指针class Solution {public: string minWindow(string s, string t) { int m=t.size(); int n=s.size(); int i,j; unordered_map mp; for(i=0;i<m;i++)原创 2016-09-14 09:57:24 · 209 阅读 · 0 评论 -
44. Wildcard Matching
动态规划bool dp[100000];bool rp[100000];class Solution {public: bool isMatch(string s, string p) { int m=p.size(),n=s.size(); int i,j; memset(dp, 0, sizeof(dp)); m原创 2016-09-13 23:04:18 · 210 阅读 · 0 评论 -
28. Implement strStr()
方法1:直接匹配,复杂度O(m*n)class Solution {public: int strStr(string haystack, string needle) { int n=haystack.size(); int m=needle.size(); if(n<m) return -1;原创 2016-09-11 20:13:24 · 151 阅读 · 0 评论 -
20. Valid Parentheses
使用栈class Solution {public: bool isValid(string s) { stack st; int n=s.size(); int i; for(i=0;i<n;i++) { if(s[i]==')') {原创 2016-09-11 20:11:05 · 128 阅读 · 0 评论 -
22. Generate Parentheses
递归class Solution {public: void generate(vector& Parens,string Paren,int left,int right,int n) { if(left==right&&left==n) { Parens.push_back(Paren); r原创 2016-09-11 20:00:46 · 143 阅读 · 0 评论 -
17. Letter Combinations of a Phone Number
递归class Solution {public: void GetLetter(vector& letters,string letter,vector& mp,string& digits,int sta) { int n=digits.size(); int i,j; if(sta<n&&(digits[sta]==1||原创 2016-09-11 19:31:16 · 278 阅读 · 0 评论 -
14. Longest Common Prefix
class Solution {public: string longestCommonPrefix(vector& strs) { string tmp=""; int n=strs.size(); if(n==0) return tmp; int i,j; for(i=0;i<st原创 2016-09-11 19:29:58 · 187 阅读 · 0 评论 -
13. Roman to Integer
方法1:class Solution {public: int romanToInt(string s) { vector Roman={'I','V','X','L','C','D','M'}; vector dig={1,5,10,50,100,500,1000}; map mp; int n=dig.size();原创 2016-09-11 19:27:48 · 186 阅读 · 0 评论 -
12. Integer to Roman
class Solution {public: string intToRoman(int num) { vector Roman={"I","V","X","L","C","D","M"}; vector dig={1,5,10,50,100,500,1000}; string tmp=""; int n=dig.size原创 2016-09-11 11:51:43 · 258 阅读 · 0 评论 -
10. Regular Expression Matching
首先将匹配字符分成4个部分[,] [.*],[字母+*],[字母]bool dp[1000][1000];class Solution {public: bool isMatch(string s, string p) { memset(dp,0,sizeof(dp)); int n=s.size(),m=p.size(); v原创 2016-09-09 15:24:17 · 156 阅读 · 0 评论 -
383. Ransom Note
方法1:class Solution {public: bool canConstruct(string ransomNote, string magazine) { int alp[30],alpc[30]; memset(alp,0,sizeof(alp)); memset(alpc,0,sizeof(alpc));原创 2016-08-21 16:51:06 · 163 阅读 · 0 评论 -
32. Longest Valid Parentheses
方法1:使用栈struct P{ int s,e; P(int l,int r):s(l),e(r){}};class Solution {public: int longestValidParentheses(string s) { stack st; stack vs; stack ans;原创 2016-09-12 15:28:57 · 153 阅读 · 0 评论 -
30. Substring with Concatenation of All Words
方法1:使用unordered_mapclass Solution {public: bool valid(string s,int sta,vector& words,unordered_map& mp,vector& vt,int m) { int n=words[0].size(); vector num(vt.begin(),vt原创 2016-09-12 17:07:41 · 165 阅读 · 0 评论 -
72. Edit Distance
动态规划class Solution {public: int minDistance(string word1, string word2) { int dp[1000][1000]; int m=word1.size(); int n=word2.size(); int i,j; memset(dp,原创 2016-09-13 22:40:23 · 253 阅读 · 0 评论 -
65. Valid Number
当科学计数法时必须前面后面都有数字,且可以带正负号,前面的digit 可以是小数{digit}e{digit}example:2e10-2e-32.3e14.e2.2e81-.2e91小数的表示1.01..1-.1+.8class Solution {public: bool isdig(string s)原创 2016-09-13 21:14:18 · 215 阅读 · 0 评论 -
71. Simplify Path
class Solution {public: string simplifyPath(string path) { int n=path.size(); vector paths; int i,j; for(i=0;i<n;i++) { string tmp="";原创 2016-09-13 21:04:39 · 166 阅读 · 0 评论 -
49. Group Anagrams
class Solution {public: vector> groupAnagrams(vector& strs) { map mp; vector> ans; int n=strs.size(); int i,j; for(i=0;i<n;i++) { int t原创 2016-09-13 21:03:18 · 155 阅读 · 0 评论 -
58. Length of Last Word
class Solution {public: int lengthOfLastWord(string s) { int n=s.size(); int i=n-1,j; while(i>=0&&s[i]==' ') { i--; } j=i; whil原创 2016-09-13 21:00:03 · 132 阅读 · 0 评论 -
67. Add Binary
class Solution {public: string addBinary(string a, string b) { string ans="",res=""; int n=a.size(); int m=b.size(); if(!n||!m) return ans;原创 2016-09-13 20:56:02 · 170 阅读 · 0 评论