CONTENTS
LeetCode 66. Plus one (simple)
[Title description]
Given a nonnegative integer represented by a nonempty array of integers, add one to the number . The highest digit is stored at the beginning of the array, and each element in the array stores only a single number. You can assume that other than the integer 0, the integer will not start with zero.
【Example 1】
输入:digits = [1,2,3]
输出:[1,2,4]
解释:输入数组表示数字 123。
【Example 2】
输入:digits = [4,3,2,1]
输出:[4,3,2,2]
解释:输入数组表示数字 4321。
【Example 3】
输入:digits = [0]
输出:[1]
【hint】
1 ≤ digits length ≤ 100 1\and digits.length\and 1001≤digits.length≤100
0 ≤ d i g i t s [ i ] ≤ 9 0\le digits[i]\le 9 0≤digits[i]≤9
【analyze】
Simple high-precision template questions.
【Code】
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int t = 1;
for (int i = digits.size() - 1; t && i >= 0; i--)
{
t += digits[i];
digits[i] = t % 10;
t /= 10;
}
if (t) digits.insert(digits.begin(), 1);
return digits;
}
};
LeetCode 67. Binary sum (simple)
[Title description]
Given two binary strings a
and b
, return their sum as a binary string.
【Example 1】
输入:a = "11", b = "1"
输出:"100"
【Example 2】
输入:a = "1010", b = "1011"
输出:"10101"
【hint】
1 ≤ a length , b length ≤ 1 0 4 1\and a.length, b.length\and 10^41≤a.length,b.length≤104
a
andconsistingb
only of the characters'0'
or ,without leading zerosif not'1'
"0"
【analyze】
It is also a high-precision template question, just note that it is binary.
【Code】
class Solution {
public:
string addBinary(string a, string b) {
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
string res;
for (int i = 0, t = 0; i < a.size() || i < b.size() || t; i++)
{
if (i < a.size()) t += a[i] - '0';
if (i < b.size()) t += b[i] - '0';
res += to_string(t % 2);
t /= 2;
}
reverse(res.begin(), res.end());
return res;
}
};
LeetCode 68. Align text left and right (difficult)
[Title description]
Given an array of words words
and a length maxWidth
, reformat the words so that they become maxWidth
text with exactly characters per line and justified left and right ends.
You should use a greedy algorithm to place a given word; that is, put as many words as possible into each line. If necessary, ' '
pad with spaces so that each line contains exactly maxWidth
characters.
The requirement is to distribute the number of spaces between words as evenly as possible. If the spaces between words in a line are not evenly distributed, more spaces are placed on the left than on the right.
The last line of text should be left-justified with no extra spaces inserted between words.
Notice:
- A word is a sequence of characters consisting of non-space characters.
- The length of each word is greater than 0 and less than or equal to
maxWidth
. - The input word array
words
contains at least one word.
【Example 1】
输入: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16
输出:
[
"This is an",
"example of text",
"justification. "
]
【Example 2】
输入:words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
输出:
[
"What must be",
"acknowledgment ",
"shall be "
]
解释: 注意最后一行的格式应为 "shall be " 而不是 "shall be",
因为最后一行应为左对齐,而不是左右两端对齐。
第二行同样为左对齐,这是因为这行只包含一个单词。
【Example 3】
输入:words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"],maxWidth = 20
输出:
[
"Science is what we",
"understand well",
"enough to explain to",
"a computer. Art is",
"everything else we",
"do "
]
【hint】
1 ≤ words length ≤ 300 1\and words.length\and 3001≤words.length≤300
1 ≤ words[i] length ≤ 20 1\lewords[i].length\le201≤words[i].length≤20
words[i]
consists of lowercase English letters and symbols
1 ≤ max W idth ≤ 100 1\le maxWidth\le 1001≤maxWidth≤100
w o r d s [ i ] . l e n g t h ≤ m a x W i d t h words[i].length\le maxWidth words[i].length≤maxWidth
【analyze】
Suppose we have ttT spaces need to be added, withccc vacancies. Since the spaces need to be distributed as evenly as possible, each vacancy needs to have at least⌊ tc ⌋ \lfloor \frac{t}{c} \rfloor⌊ct⌋ spaces, the extrat % ct\% ct % c spaces are added to each gap in sequence starting from the left (one space is added to each gap).
Also note that when there is only one word in a line or it is the last line, it needs to be left aligned and filled with spaces on the right.
【Code】
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> res;
for (int i = 0; i < words.size(); i++)
{
int len = words[i].size(); // 当前行的总长度,每个单词间有一个空格
int j = i + 1; // 一行能够放置最多单词的右边界
while (j < words.size() && len + 1 + words[j].size() <= maxWidth)
len += 1 + words[j].size(), j++;
string line;
if (j == i + 1 || j == words.size()) // 只有一个单词或者最后一行
{
line += words[i];
for (int k = i + 1; k < j; k++) line += ' ' + words[k];
while (line.size() < maxWidth) line += ' '; // 长度不够在右边补空格
}
else // 否则需要两端对齐
{
int c = j - i - 1, t = maxWidth - len + c; // c表示空位数,t表示需要填充的空格数
int k = 0;
while (k < t % c) line += words[i + k] + string(t / c + 1, ' '), k++;
while (k < c) line += words[i + k] + string(t / c, ' '), k++;
line += words[i + k]; // 最后一个单词
}
res.push_back(line);
i = j - 1;
}
return res;
}
};
LeetCode 69. Square root of x (simple)
[Title description]
Given a nonnegative integer x
, compute and return x
the arithmetic square root of .
Since the return type is an integer, only the integer part of the result is retained , and the decimal part will be rounded off . NOTE: Any built-in exponential functions and operators, such as or
, are not allowed .pow(x, 0.5)
x ** 0.5
【Example 1】
输入:x = 4
输出:2
【Example 2】
输入:x = 8
输出:2
解释:8 的算术平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。
【hint】
0 ≤ x ≤ 2 31 − 1 0\le x\le 2^{31}-10≤x≤231−1
【analyze】
Dichotomous answer template question, find y 2 ≤ xy^2\le xy2≤The largestyy of xy is enough, that is, every time we are at0 ∼ x 0\sim x0∼Dividemid mid from x intervalmi d , judgemid midIs mi d greater than or equal toxxx。
【Code】
class Solution {
public:
int mySqrt(int x) {
int l = 0, r = x;
while (l < r)
{
int mid = l + 1ll + r >> 1; // 1ll防止l + r溢出
// mid * mid <= x中可能会溢出,因此需要移一下位
if (mid <= x / mid) l = mid;
else r = mid - 1;
}
return r;
}
};
LeetCode 70. Climb stairs (easy)
[Title description]
Suppose you are climbing stairs. It takes n
steps to get to the top of the building.
You can climb 1
or 2
steps at a time. How many different ways can you climb to the top of a building?
【Example 1】
输入:n = 2
输出:2
解释:有两种方法可以爬到楼顶。
1. 1 阶 + 1 阶
2. 2 阶
【Example 2】
输入:n = 3
输出:3
解释:有三种方法可以爬到楼顶。
1. 1 阶 + 1 阶 + 1 阶
2. 1 阶 + 2 阶
3. 2 阶 + 1 阶
【hint】
1 ≤ n ≤ 45 1\le n\le 451≤n≤45
【analyze】
Assume f[i]
that jump to iiThe number of methods for the i- th step, then we can start from thei − 2 i-2i−2 ori − 1 i-1i−Jump over the firstf[i] = f[i - 2] + f[i - 1]
step, that is , this transfer equation is the Fibonacci sequence, so we only need to use two variables to scroll in a loop. The method number at the starting point is 1. The first step can only be jumped from the starting point, so the method number is also 1, and then we solve backward to the n− 1 n-1n−1 item is enough.
【Code】
class Solution {
public:
int climbStairs(int n) {
int a = 1, b = 1;
while (--n) // --n只循环n-1次
{
int c = a + b;
a = b, b = c;
}
return b;
}
};