23. 合并K个升序链表
给你一个链表数组,每个链表都已经按升序排列。
请你将所有链表合并到一个升序链表中,返回合并后的链表。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
#def mergeKLists(self, lists: List[ListNode]) -> ListNode:
'''
def mergeKLists(self, lists: List[ListNode]):
l=len(lists)
t=ListNode(0)
t.val=0
c=t
empty_flag=0
j=0
un_emp_s=0
for i in range(l):
if lists[i]!=None:
un_emp_s=un_emp_s+1
print("un_emp_s=",un_emp_s)
p=[ListNode(0)]*un_emp_s
for i in range(l):
#print("lists[i]",lists[i])
if lists[i]!=None:
p[j]=lists[i]
j=j+1
if empty_flag==0:
empty_flag=1
#print("p[i]",p[i])
if empty_flag==0 or l==0:
return None
#print("len(p)",len(p))
#print("p=",p)
while(1):
s=0
for i in range(len(p)):
if p[i]!=None:
k=i
s=s+1
if s==1:
t.next=p[k]
break
temp=0
for i in range(len(p)):
#print("i=",i)
if p[i]!=None and p[temp]!=None:
#print("p[i].val=",p[i].val)
if p[i].val<p[temp].val:
temp=i
#print("p[temp].val=",p[temp].val)
if p[temp]!=None:
t.next=p[temp]
t=t.next
#print("t.val=",t.val)
p[temp]=p[temp].next
c=c.next
return c
'''
#无序地放进list,list进行排序,list放进ListNode
def mergeKLists(self, lists):
"""
:type lists: List[ListNode]
:rtype: ListNode
"""
num_list = []
if lists == []:
return []
else:
for i in lists:
while i:
num_list.append(i.val)
i = i.next
num_list.sort()
head = ListNode(0)
first = head
for i in num_list:
head.next = ListNode(i)
head = head.next
return first.next
4. 寻找两个正序数组的中位数
给定两个大小分别为 m
和 n
的正序(从小到大)数组 nums1
和 nums2
。请你找出并返回这两个正序数组的 中位数 。
算法的时间复杂度应该为 O(log (m+n))
。
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int l1=nums1.size();
int l2=nums2.size();
int l3=0;
int mod_flag=0;
int loc=0;
int k=0;
int ki=0;
int i=0;
int j=0;
float re=0;
vector<int> nums;
l3=l1+l2;
//cout<<"l1="<<l1<<endl;
//cout<<"l2="<<l2<<endl;
//cout<<"l3="<<l3<<endl;
mod_flag=l3%2;
//cout<<"mod_flag="<<mod_flag<<endl;
if(mod_flag==1)
{
k=(l3/2)+1;
cout<<"k="<<k<<endl;
while(ki<k)
{
if(i<l1 && j<l2)
{
if(nums1[i]<=nums2[j])
{
nums.push_back(nums1[i]);
cout<<"val="<<nums1[i]<<endl;
i++;
}
else
{
nums.push_back(nums2[j]);
cout<<"val="<<nums2[j]<<endl;
j++;
}
}
else if(i<l1)
{
nums.push_back(nums1[i]);
cout<<"val="<<nums1[i]<<endl;
i++;
}
else if(j<l2)
{
nums.push_back(nums2[j]);
cout<<"val="<<nums2[j]<<endl;
j++;
}
ki++;
cout<<"ki="<<ki<<endl;
}
cout<<"test1"<<endl;
re=nums[ki-1];
}
else if(mod_flag==0)
{
k=(l3/2)+1;
cout<<"k="<<k<<endl;
while(ki<k)
{
if(i<l1 && j<l2)
{
if(nums1[i]<=nums2[j])
{
nums.push_back(nums1[i]);
cout<<"val="<<nums1[i]<<endl;
i++;
}
else
{
nums.push_back(nums2[j]);
cout<<"val="<<nums2[j]<<endl;
j++;
}
}
else if(i<l1)
{
nums.push_back(nums1[i]);
cout<<"val="<<nums1[i]<<endl;
i++;
}
else if(j<l2)
{
nums.push_back(nums2[j]);
cout<<"val="<<nums2[j]<<endl;
j++;
}
ki++;
cout<<"ki="<<ki<<endl;
}
cout<<"test1"<<endl;
//cout<<"nums="<<nums[ki-1]<<endl;
//cout<<"nums="<<
re=(float(nums[ki-1])+float(nums[ki-2]) )/2;
}
cout<<"test"<<endl;
return re;
}
};
42. 接雨水
给定 n
个非负整数表示每个宽度为 1
的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
class Solution {
public:
int trap(vector<int>& height) {
if(height.size()==0)
{
return 0;
}
//寻找最大值的位置
int max=height[0];
int max_sta=0;
for(int i=1;i<height.size();i++)
{
if(height[i]>max)
{
max=height[i];
max_sta=i;
}
}
cout<<"max="<<max<<endl;
cout<<"max_sta="<<max_sta<<endl;
int re=0;
int l_max=height[0];
for(int i=1;i<max_sta;i++)
{
if(height[i]>l_max)
{
l_max=height[i];
}
else
{
re=re+(l_max-height[i]);
}
}
int r_max=height[height.size()-1];
for(int i=height.size()-1;i>max_sta;i--)
{
if(height[i]>r_max)
{
r_max=height[i];
}
else
{
re=re+(r_max-height[i]);
}
}
return re;
}
};
72. 编辑距离
给你两个单词 word1
和 word2
, 请返回将 word1
转换成 word2
所使用的最少操作数 。
你可以对一个单词进行如下三种操作:
- 插入一个字符
- 删除一个字符
- 替换一个字符
class Solution {
public:
int minDistance(string word1, string word2) {
int l1=word1.size();
int l2=word2.size();
vector<vector<int>> dp(l1+1,vector<int>(l2+1,0));
dp[0][0]=0;
for(int i=0;i<=l1;i++)
{
dp[i][0]=i;
}
for(int i=0;i<=l2;i++)
{
dp[0][i]=i;
}
//cout<<"dp[0][0]="<<dp[0][0]<<endl;
for(int i=1;i<=l1;i++)
{
for(int j=1;j<=l2;j++)
{
//cout<<"word1[i]="<<word1[i]<<endl;
//cout<<"word2[j]="<<word1[j]<<endl;
if(word1[i-1]==word2[j-1])
{
dp[i][j]=dp[i-1][j-1];
}
else
{
dp[i][j]=min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1]))+1;
}
//cout<<"i="<<i<<endl;
//cout<<"j="<<j<<endl;
//cout<<"dp[i][j]="<<dp[i][j]<<endl;
}
}
cout<<"re="<<dp[l1][l2]<<endl;
return dp[l1][l2];
}
};
84. 柱状图中最大的矩形
给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
求在该柱状图中,能够勾勒出来的矩形的最大面积。
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int len=heights.size();
int l1,l2;
int temp=0;;
int re=0;
int minHeight=0;
int flagisone=1;
for(int i=0;i<len;i++)
{
if(heights[i]!=1)
{
flagisone=0;
}
}
if(flagisone==1)
{
return len;
}
for(int i=0;i<len;i++)
{
minHeight=heights[i];
for(int j=i;j>=0;j--)
{
l1=i-j+1;
if(heights[j]<minHeight)
{
minHeight=heights[j];
}
temp=l1*minHeight;
if(temp>re)
{
re=temp;
}
}
}
return re;
}
};
297. 二叉树的序列化与反序列化
序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。
请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。
提示: 输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
string re;
//re=re+"0";
//cout<<"re="<<re<<endl;
string l_str;
string r_str;
string root_str;
if(root!=NULL)
{
//cout<<"root->val="<<root->val<<endl;
root_str=to_string(root->val)+"_";
l_str=serialize(root->left);
r_str=serialize(root->right);
}
else
{
return "#_";
}
re=root_str+l_str+r_str;
//cout<<"re="<<re<<endl;
return re;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
vector<string> re;
cout<<"data="<<data<<endl;
//字符串转化成向量
for(int i = 0, j = 0; i < data.size() && j < data.size(); j++){
if(data[j] == '_'){
cout<<"data.substr(i,j-i)="<<data.substr(i,j-i)<<endl;
re.push_back(data.substr(i,j-i));
i = j+1;
}
}
//cout<<"re="<<re<<endl;
int i = 0;
TreeNode* root = creatTree(re, i);
return root;
}
private:
TreeNode* creatTree(vector<string>& re, int& i){
TreeNode *root = NULL;
if(re[i] == "#") return root;
root = new TreeNode(atoi(re[i].c_str()));
root->left = creatTree(re, ++i);
root->right = creatTree(re, ++i);
return root;
}
};
// Your Codec object will be instantiated and called as such:
// Codec ser, deser;
// TreeNode* ans = deser.deserialize(ser.serialize(root));