
LeetCode-string
peanwang
这个作者很懒,什么都没留下…
展开
-
LeetCode 804. Unique Morse Code Words
804. Unique Morse Code Words这应该属于考简单hash。毕竟Morse Code也算是hash函数。迭代vector<string>。 把string -> Morse Code。用set除重。class Solution {public: int uniqueMorseRepresentations(vector<string>...原创 2018-10-18 16:12:04 · 104 阅读 · 0 评论 -
LeetCode 387. First Unique Character in a String
387. First Unique Character in a Stringclass Solution {public: int firstUniqChar(string s) { int alp[26],len = s.size(); fill(begin(alp),end(alp),-1); for(int i=0;i&lt;le...原创 2018-11-12 21:12:31 · 157 阅读 · 0 评论 -
LeetCode 58. Length of Last Word
58. Length of Last Wordclass Solution {public: int lengthOfLastWord(string s) { int len = 0; auto it = s.rbegin(); while (it != s.rend()) { if (*it == ' ') it++; ...原创 2018-11-14 17:30:53 · 105 阅读 · 0 评论 -
LeetCode 929. Unique Email Addresse
929. Unique Email Addressesclass Solution {public: int numUniqueEmails(vector&lt;string&gt;&amp; emails) { set&lt;string&gt; unique; for(auto str:emails){ string resu...原创 2018-11-08 17:44:43 · 209 阅读 · 1 评论 -
LeetCode 13. Roman to Integer
13. Roman to Integerclass Solution {public: int romanToInt(string s) { const map&lt;char,int&gt; roman{{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}}; int sum=...原创 2018-11-11 15:41:04 · 126 阅读 · 0 评论 -
LeetCode 696. Count Binary Substrings
696. Count Binary Substringsclass Solution {public: int countBinarySubstrings(string s) { int count=0,size=0; char prev=s[0]; vector&lt;int&gt; group; for(auto x...原创 2018-11-04 11:47:48 · 153 阅读 · 0 评论 -
LeetCode 242. Valid Anagram
242. Valid Anagramclass Solution {public: bool isAnagram(string s, string t) { int alp1[26]{0},alp2[26]{0}; for(auto ch:s) alp1[ch-'a']++; for(auto ch:t) ...原创 2018-11-09 16:00:31 · 90 阅读 · 0 评论 -
LeetCode 383. Ransom Note
383. Ransom Note就是问第二个字符串magazine中的所有字符能不能组成第一个字符串ransomNote。统计两个字符串的a-z字母数量就行了。class Solution {public: bool canConstruct(string ransomNote, string magazine) { int map1[26]{0},map2[26]...原创 2018-11-01 20:21:08 · 127 阅读 · 0 评论 -
LeetCode 14. Longest Common Prefix
14. Longest Common Prefixclass Solution {public: string longestCommonPrefix(vector&lt;string&gt;&amp; strs) { string result; int minlength=0x65536; for(auto x:strs){ ...原创 2018-11-01 15:38:32 · 112 阅读 · 0 评论 -
LeetCode 125. Valid Palindrome
125. Valid Palindrome第一份我写的(垃圾不用看)。第二份大神写的:建议看看class Solution {public: bool isPalindrome(string s) { string change; for(auto x:s) if(isalnum(x)){ if(...原创 2018-10-21 21:13:59 · 99 阅读 · 0 评论 -
LeetCode 893. Groups of Special-Equivalent Strings
893. Groups of Special-Equivalent Stringsclass Solution {public: int numSpecialEquivGroups(vector&lt;string&gt;&amp; A) { set&lt;vector&lt;int&gt;&gt; Set; for(auto x:A){原创 2018-10-21 17:27:20 · 229 阅读 · 0 评论 -
LeetCode 415. Add Strings
415. Add Strings和Add binary一模一样啊。class Solution {public: string addStrings(string num1, string num2) { string result; int N1 = num1.size(),N2 =num2.size(),carry=0; for(i...原创 2018-10-25 17:43:22 · 90 阅读 · 0 评论 -
LeetCode 67. Add Binary
67. Add Binary这题只能迭代字符串。一个接一个地加。如果想用先把字符串转换为数字。相加后转换为字符串,是不可行的。因为测试数据会给出很长的字符串,是无法转换成数字的。Code:class Solution {public: string addBinary(string a, string b) { int carry=0,N1=a.size(),N...原创 2018-10-25 16:45:58 · 144 阅读 · 0 评论 -
LeetCode344. Reverse String
344. Reverse Stringclass Solution {public: string reverseString(string s) { reverse(s.begin(),s.end()); return s; }};原创 2018-10-20 09:55:53 · 121 阅读 · 0 评论 -
LeetCode 859. Buddy Strings
859. Buddy StringsBuddy String是指两个字符串:其中一个字符串只用交换两个字符,就能和另一个字符串相等。首先:两个字符串长度要一样,才可能是Buddy Strings.第二:要区分两个字符串是否相等:相等:可以通过交换一个字符串中两个相等的字符比如 ‘aa’ 和‘aa’(LeetCode的example也给出了这个例子)。这个时候我用set统计字符串中Uni...原创 2018-10-22 11:22:57 · 251 阅读 · 0 评论 -
LeetCode 557. Reverse Words in a String III
557. Reverse Words in a String III这题是反转字符串中的单词。关键是找到单词与单词之间的空格。在题目的Note里:有写单词与单词之间的空格只有一个。所以我的代码每次在找到空格后,只跳过一个。(这题通俗应该写一个循环,把空格都跳过去)。Note: In the string, each word is separated by single space and t...原创 2018-10-20 00:05:50 · 99 阅读 · 0 评论 -
LeetCode 709. To Lower Case
LeetCode 709. To Lower Case这题把字符串里所有大写字母改为小写字母。很多语言都自带这个函数(方法).C++里我使用了transform函数。class Solution {public: string toLowerCase(string str) { transform(str.begin(),str.end(),str.begin()...原创 2018-10-17 09:03:12 · 144 阅读 · 0 评论 -
LeetCode 657. Robot Return to Origin
657. Robot Return to Origin挺简单的模拟题。只要U和D,L和R的出现次数是一样的。Robot就还在原点。Code:class Solution {public: bool judgeCircle(string moves) { int U=0,D=0,L=0,R=0; for(auto x:moves){ ...原创 2018-10-19 15:12:20 · 240 阅读 · 0 评论 -
LeetCode 917. Reverse Only Letterse
917. Reverse Only Letters/*using C++ STL*/class Solution {public: string reverseOnlyLetters(string S) { string Q; for(auto ch:S){ if(isalpha(ch)) Q...原创 2018-12-04 08:45:40 · 170 阅读 · 0 评论