力扣题-1.6
力扣题1:6. Z 字形变换
解题思想:首先判断特殊条件,直接输出s;然后创建字符数组,遍历字符串将字符放置对应的位置,然后将数组进行拼接即可.
class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
if numRows == 1 or numRows >= len(s):
return s
result = [''] * numRows
cycle_len = 2 * (numRows - 1)
for i in range(len(s)):
remainder = i % cycle_len
index = remainder if remainder < numRows else cycle_len - remainder
result[index] += s[i]
return ''.join(result)
class Solution {
public:
string convert(string s, int numRows) {
if (numRows == 1 || numRows >= s.length()) {
return s;
}
std::vector<std::string> result(numRows, "");
int cycle_len = 2 * (numRows - 1);
for (int i = 0; i < s.length(); ++i) {
int remainder = i % cycle_len;
int index = (remainder < numRows) ? remainder : cycle_len - remainder;
result[index] += s[i];
}
std::string final_result;
for (const std::string& row : result) {
final_result += row;
}
return final_result;
}
};
力扣题2:68. 文本左右对齐
解题思想:虽然是困难题,但不是非常困难,主要考察细心和分类讨论的情况
class Solution(object):
def fullJustify(self, words, maxWidth):
"""
:type words: List[str]
:type maxWidth: int
:rtype: List[str]
"""
## 先计算每行应该有的单词放置在result中
result = []
sentense = []
temp = 0
for i in range(len(words)):
if temp + len(words[i]) >maxWidth:
temp = 0
result.append(sentense)
sentense = []
temp = temp + len(words[i])+1
sentense.append(words[i])
if sentense!= []:
result.append(sentense)
## 构造最后的输出final_result数组
final_result = []
## 遍历每个result
for i in range(len(result)):
length = 0
kong = 0
left = 0
temp = ""
## 如果不是最后一句话
if i !=len(result)-1:
## 先进行计算空格数
for j in range(len(result[i])):
length += len(result[i][j])
## 如果不是一个单词
if len(result[i])-1!=0:
kong = (maxWidth - length)/(len(result[i])-1)
left = maxWidth- length - kong*(len(result[i])-1)
print(left,kong,length)
## 然后进行拼接
for j in range(len(result[i])):
if j ==len(result[i])-1:
temp = temp+result[i][j]
else:
if left!= 0 :
temp = temp+result[i][j]+" "*kong+" "
left = left - 1
else:
temp = temp+result[i][j]+" "*kong
## 如果是一个单词
else:
left = maxWidth- length
## 然后进行拼接
temp = temp+result[i][j]+" "*left
## 如果是最后一句话
else:
for j in range(len(result[i])):
if j != len(result[i])-1:
temp = temp+result[i][j]+" "
else:
temp = temp+result[i][j]
kong = maxWidth- len(temp)
temp = temp+" "*kong
final_result.append(temp)
return final_result
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
std::vector<std::vector<std::string>> result;
std::vector<std::string> sentence;
int temp = 0;
for (int i = 0; i < words.size(); ++i) {
if (temp + words[i].length() > maxWidth) {
temp = 0;
result.push_back(sentence);
sentence.clear();
}
temp = temp + words[i].length() + 1;
sentence.push_back(words[i]);
}
if (!sentence.empty()) {
result.push_back(sentence);
}
std::vector<std::string> final_result;
for (int i = 0; i < result.size(); ++i) {
int length = 0;
int spaces = 0;
int left = 0;
std::string temp_str = "";
if (i != result.size() - 1) {
for (int j = 0; j < result[i].size(); ++j) {
length += result[i][j].length();
}
if (result[i].size() - 1 != 0) {
spaces = (maxWidth - length) / (result[i].size() - 1);
left = maxWidth - length - spaces * (result[i].size() - 1);
for (int j = 0; j < result[i].size(); ++j) {
if (j == result[i].size() - 1) {
temp_str = temp_str + result[i][j];
} else {
if (left != 0) {
temp_str = temp_str + result[i][j] + std::string(spaces + 1, ' ');
left = left - 1;
} else {
temp_str = temp_str + result[i][j] + std::string(spaces, ' ');
}
}
}
} else {
left = maxWidth - length;
temp_str = temp_str + result[i][0] + std::string(left, ' ');
}
} else {
for (int j = 0; j < result[i].size(); ++j) {
if (j != result[i].size() - 1) {
temp_str = temp_str + result[i][j] + " ";
} else {
temp_str = temp_str + result[i][j];
spaces = maxWidth - temp_str.length();
temp_str = temp_str + std::string(spaces, ' ');
}
}
}
final_result.push_back(temp_str);
}
return final_result;
}
};