每天坚持写几道leetcode,希望几个月后我就不再是小白
今天的题目是344,557,657,804
总结收获
- unordered_map,unordered_set,map和set的区别:unordered_map存储机制是哈希表,即unordered_map内部元素是无序的。map是红黑树,map中的元素是按照二叉搜索树存储,进行中序遍历会得到有序遍历。unordered_set基于哈希表,是无序的。set实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值;另外,还得保证根节点左子树的高度与右子树高度相等。平衡二叉检索树使用中序遍历算法,检索效率高于vector、deque和list等容器,另外使用中序遍历可将键值按照从小到大遍历出来。
- 在编程中要尽可能多的使用const,这样可以获得编译器的帮助,以便写出健壮性的代码。(外部引用者更为合适)
set
容器的insert
操作不需要提前判定元素存在,如下图:vector
和string
容器实现了swap
方法,内置swap
进行了优化,优于自己写的简单交换- 暂时不明白
string s = "";
和string s;
的区别,希望大佬可以指点
题目:344. Reverse String
描述:Write a function that takes a string as input and returns the string reversed.
例子:
Input: "hello"
Output: "olleh"
代码:
string Solution344::reverseString(string s)
{
int len = s.size();
if (len < 2)return s;
for (int i = 0; i < len / 2; i++)
{
swap(s[i], s[len - i - 1]);
}
return s;
}
- swap优于自己写的交换
题目:557. Reverse Words in a String III
描述:Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
例子:
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
代码:
string Solution557::reverseWords(string s)
{
int len = s.size();
int j = 0;
for (int i = 0; i < len; i++)
{
if (i + 1 == len || s[i + 1] == ' ')
{
int n = i - j + 1;
int x = j;
while (j <= x + n /2 - 1)
{
swap(s[j], s[i +x - j]);
++j;
}
j = i + 2;
}
}
return s;
}
在这道题中踩过的坑:
1. 边界条件:i指向最后一个元素的情况
2. while循环中j不断变化,循环边界需要不变边界
3. swap交换时元素的下标计算
题目:657. Judge Route Circle
描述:Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.
The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.
例子:
Input: "UD"
Output: true
Input: "LL"
Output: false
代码:
bool Solution657::judgeCircle(string moves)
{
vector<int> m(4);//L,R,U,D
for (auto &c : moves)
{
if (c == 'L')m[0]++;
else if (c == 'R')m[1]++;
else if (c == 'U')m[2]++;
else m[3]++;
}
return (m[0] == m[1] && m[2] == m[3]);
//unordered_map<char, int>com;
//for (auto &c : moves)com[c]++;
//return com['L'] == com['R'] && com['U'] == com['D'];
}
尝试了一下注释掉的方法二runtime很差劲,可能是因为查找比较费时间
题目:804. Unique Morse Code Words
描述:International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: “a” maps to “.-“, “b” maps to “-…”, “c” maps to “-.-.”, and so on.
For convenience, the full table for the 26 letters of the English alphabet is given below:
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, “cab” can be written as “-.-.-….-“, (which is the concatenation “-.-.” + “-…” + “.-“). We’ll call such a concatenation, the transformation of a word.
Return the number of different transformations among all words we have.
例子:
Example:
Input: words = ["gin", "zen", "gig", "msg"]
Output: 2
Explanation:
The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."
There are 2 different transformations, "--...-." and "--...--.".
Note:
- The length of words will be at most 100.
- Each words[i] will have length in range [1, 12].
- words[i] will only consist of lowercase letters.
代码:
int Solution804::uniqueMorseRepresentations(vector<string>& words)
{
//int len = words.size();
//if (len < 2)return len;
//vector<string> map = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
//set<string> compare;
//for (string s : words)
//{
// string temp = "";
// for (char c : s)
// {
// temp += map[c - 'a'];
// }
// //if (compare.find(temp) == compare.end())
// compare.insert(temp);
//}
//return compare.size();
unordered_set<string> compare;
for (string &s : words)
{
string temp;
for (char &c : s)
{
temp += map[c - 'a'];
}
//if (compare.find(temp) == compare.end())
compare.insert(temp);
}
return compare.size();
}
注释掉的代码是我自己写的,下面的代码是看了runtime比较好的答案,经过对比多次提交比较得到一下几个猜想,有待验证:
1. unordered_set优于set
2. public定义compare优于函数内定义
3. compare前加上const关键字优于不加
4. for循环加上&优于不加
5. set执行insert不需要提前判断集合中是否有该元素
6. temp不初始化不会报错,且优于初始化
结束
更多内容请浏览我的个人博客:AlisaBen