- 合并两个有序序列,尽量利用已有空间。
(1)合并有序数组:
Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6],其中nums1数组无限大。
不要太纠结于不能分配空间上面,从新空间的位置从后向前,放两个数组中较大的元素。这样避免了插入时移动元素,新空间就是nums1数组,因为从后面向前不会造成刚放置的元素被覆盖的情况。
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i = m - 1, j = n - 1, k = m + n - 1;
while (i >= 0 && j >= 0) {
if (nums1[i] > nums2[j]) nums1[k--] = nums1[i--];
else nums1[k--] = nums2[j--];
}
while (j >= 0) nums1[k--] = nums2[j--];
while (i >= 0) nums1[k--] = nums1[i--];//可以省略,因为只剩下nums元素的话就不用赋值了。
}
};
(2) 序列是链表的形式
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
借助头指针和新链表的指针,只指向较小元素,这样完全不影响两链表的遍历指针可以画图看。
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *head = new ListNode(-1), *cur = head;
while (l1 && l2)
{
if (l1->val < l2->val)
{
cur->next = l1;
l1 = l1->next;
}
else
{
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
cur->next = l1 ? l1 : l2;
return head->next;
}
};
- 大数相乘
乘法的步骤:数字与第二个数组逐位相乘,然后错位相加。我们定义一个m+n长的数组,当前位放相乘元素,下一位放进位。逐位相乘,然后错位相加都可以通过下标实现。
//4. Multiply Strings
//大数相乘,用数组来存放各位数字,数组长度是两个数的长度之和。
class Solution {
public:
string multiply(string num1, string num2) {
vector<int> result(num1.length() + num2.length(), 0);
for(int i = num1.length()-1; i >= 0; i--){
int cum = 0;
for(int j= num2.length()-1; j >= 0; --j){
int tmp = cum + (num2[j]-'0') * (num1[i]-'0');
result[i + j + 1] += tmp % 10;
cum = tmp / 10 + result[i + j + 1] / 10;
result[i + j + 1] = result[i + j + 1] % 10;
}
if(cum) result[i] = cum;
}
string str;
for(int i=0; i < result.size(); i++){
if(result[i] == 0 && str.empty())
continue;
str += result[i] + '0';
}
return str.empty() ? "0" : str;
}
};
-
对于字符串,元素是ascll码(0-127),所以数组可以作为表征。如需要字典key为char时可以用数组代替字典;如判断同素异构字符串时,可用各元素的计数数组转成string作为同素异构字符串的表示。
-
linux路径简化
(1)多个“/”只能存在一个,皆为不能有“/”,最上层路径是根“/”
(2).表示当前,…表示上一级。
思路:用分割字符串做就好了,把斜杠之间的目录名放到数组里,碰到…退出一层目录。
71. Simplify Path
Input: "/a//bc/d//././/.."
Output: "/a/b/c"
/*简化路径的规则是:1.多个斜杠只能保留一个 2.末尾不能有斜杠(如果不是根目录的话) 3.遇到..返回上一层目录。
*/
class Solution {
//
public:
string simplifyPath(string path) {
vector<string> res;
int start = 0, end = 0;
while (end < path.length())
{
while (end < path.length() && path[end] == '/') end++;
start = end;
while (end < path.length() && path[end] != '/') end++;
if (end != start)
{
string s = path.substr(start, end - start);
if (s == ".." && res.size()) res.pop_back();
else if (s != "." && s!= "..") res.push_back(s);
start = end;
}
}
string res_str;
for (int i = 0; i < res.size(); i++)
{
res_str += "/" + res[i];
}
return res_str.size() == 0 ? "/" : res_str;
}
};
进制转换:
十进制–>其他进制:除x逆序取余。如转16进制,除16逆序取余,如果余数>=10,用A~F表示。
其他进制–>十进制:从低位到高位一次乘以x的n次方。如16进制 x2x1:x1 * 16^0 + x2* 16^1+ …
string c16(int num)
{
char str[7] = "ABCDEF";
string res;
vector<int> t;
while (num)
{
t.push_back(num % 16);
num = num / 16;
}
for (int i = t.size()-1; i >= 0; i--)
{
if (t[i] < 10)
res += t[i] + '0';
else
res += str[t[i] - 10];
}
return res;
}
int c10(string num)
{
int res = 0, c = 1;
for (int i = num.size() - 1; i >= 0 ; i--)
{
if (num[i] >= 'A'&& num[i] <= 'F')
res += c * (num[i] - 'A' + 10);
else
res += c * (num[i] - '0');
c = 16 * c;
}
return res;
}
- 有序矩阵寻找目标值
矩阵每行和每列元素是递增的,但是每行的结尾元素与下一行的开始元素大小没有任何关联。整体思路:寻找特殊位置,根据该位置元素大小y确定行列i’don移动方向。
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if(matrix.empty() || matrix[0].empty()) return false;
int m = matrix.size(), n= matrix[0].size(), row = m-1, col = 0;
while(row >= 0 && col < n){
if(matrix[row][col] > target)
--row;
else if(matrix[row][col] < target)
++col;
else
return true;
}
return false;
}
};