【LeetCode81-90】三道链表,两道找最大面积的hard题,一道二叉树的hard题和一些找自信题……

本文解析了一系列经典编程挑战题,包括旋转数组搜索、链表操作、直方图最大矩形、Scramble字符串判断等,提供了详细的算法思路及实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

81.寻找一个顺序但有一定旋转数组的一个数(允许重复)


这算作弊么……

class Solution {  
public:  
    bool search(vector<int>& nums, int target) {  
    vector<int>::iterator location = find( nums.begin( ), nums.end( ), target ); //查找target  
    if ( location == nums.end( ) ) //没找到  
        return false;  
    return true; 
    }  
}; 


82.删除链表里重复的数字


Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
	ListNode* deleteDuplicates(ListNode* head) {
		ListNode result(0);
		ListNode* temp1 = &result;
		ListNode* temp2 = head;
		while (temp2) {
			int j = 0;
			while (temp2&&temp2->next && (temp2->val == temp2->next->val)) { temp2 = temp2->next; ++j; }
			if (j == 0) 
			{ temp1->next = temp2; temp1 = temp1->next; }
			temp2 = temp2->next;
			if (!temp2)temp1->next = temp2;
		}
		return result.next;
	}
};

83.删除顺序链表里多余的重复的(上面一条是全删掉,这一条保留一个)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode *temp=head;
        while(temp&&temp->next){
            while(temp&&temp->next&&temp->next->val==temp->val)temp->next=temp->next->next;
            temp=temp->next;
        }
        return head;
    }
};


84.直方图寻找最大矩形


Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].


The largest rectangle is shown in the shaded area, which has area = 10 unit.

For example,
Given heights = [2,1,5,6,2,3],
return 10.


我的方法有些无赖了…但貌似也算击败了小半的人………

分了两条路(第一条判断数组是否是顺序,如果是,很好处理,这种事为了应对几百个1的那种例子……)

另一条就是遍历所有的位置,找出左右不比它小的位置也就是求出每个位置的宽,再计算总和,很暴力

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int result=0;
        int l=heights.size();
        int way=0;
        if(is_sorted(heights.begin(),heights.end()))way=1;
        if(way){
             for(int i=0;i<heights.size();++i) result=max(result,(l-i)*heights[i]);
        }
        else{
        for(int i=0;i<heights.size();++i){
            //往右找
            int length=0;
            for(int j=i;j<heights.size();++j){
                if(heights[j]>=heights[i])length++;
                else break;
            }
            for(int j=i-1;j>=0;--j){
                if(heights[j]>=heights[i])length++;
                else break;
            }
            result=max(result,length*heights[i]);
        }}
        return result;
    }
};



85.找出0,1组成的矩阵里最大面积的全部是1的矩形

For example, given the following matrix:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
返回6


借用上一题的结果……先转化为直方图,13ms

We can regard a matrix as many histograms. For example, given a matrix below:

1 0 1 0

0 1 0 1

0 1 1 0

1 0 1 0

1 0 1 1

From top to bottom, we can find these histograms:

Number 1: 1 0 1 0

Number 2: 0 1 0 1

Number 3: 0 2 1 0

Number 4: 1 0 2 0

Number 5: 2 0 3 1


class Solution {
public:
    int maximalRectangle(vector<vector<char>>& matrix) {
        int result=0;
        if(matrix.empty()||matrix[0].empty())return result;
        for(int i=0;i<matrix.size();++i){
            for(int j=0;j<matrix[0].size();++j){
                if(i==0)matrix[i][j]-=48;
                else {matrix[i][j]=matrix[i][j]=='0'?0:(matrix[i-1][j]+1);}
            }
            result=max(result,largestRectangleArea(matrix[i]));
        }
        return result;
    }
        int largestRectangleArea(vector<char>& heights) {  
        int result=0;  
        for(int i=0;i<heights.size();++i){  
            //往右找  
            int length=0;  
            for(int j=i;j<heights.size();++j){  
                if(heights[j]>=heights[i])length++;  
                else break;  
            }  
            for(int j=i-1;j>=0;--j){  
                if(heights[j]>=heights[i])length++;  
                else break;  
            }  
            result=max(result,length*heights[i]);  
        }  
        return result;  
    }  
    
};


86.单链表拆成两半(比x小的和不小于的)

For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        ListNode result1(0),result2(0);
        ListNode *temp1=&result1;
        ListNode *temp2=&result2;
        while(head){
            if(head->val<x){temp1->next=head;temp1=temp1->next;}
            else {temp2->next=head;temp2=temp2->next;}
            head=head->next;
        }
        temp1->next=result2.next;
        temp2->next=NULL;
        return result1.next;
    }
};

87.(hard)判断两个String是否满足交换条件Scramble String


Below is one possible representation of s1 = "great":

    great
   /    \
  gr    eat
 / \    /  \
g   r  e   at
           / \
          a   t

To scramble the string, we may choose any non-leaf node and swap its two children.

For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".

    rgeat
   /    \
  rg    eat
 / \    /  \
r   g  e   at
           / \
          a   t

We say that "rgeat" is a scrambled string of "great".

Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".

    rgtae
   /    \
  rg    tae
 / \    /  \
r   g  ta  e
       / \
      t   a

We say that "rgtae" is a scrambled string of "great".

Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.

Subscribe to see which companies asked this question.


不断迭代,关键是理解清楚题目意思

构成的树只有同级的子节点可以互相调换!且初始的树是随机的,只要满足叶子不为空就好!


class Solution {
public:
	bool isScramble(string s1, string s2) {
	    if(s1==s2)return true;
	    int length=s1.size();
	    if(help(s1,s2)){
	        for(int i=1;i<length;++i){
	            if(isScramble( s1.substr(0,i),s2.substr(0,i))&&isScramble( s1.substr(i),s2.substr(i)))return true;
	            if(isScramble( s1.substr(0,i),s2.substr(length-i,i))&&isScramble( s1.substr(i),s2.substr(0,length-i)))return true;	    =
	        }
	    }
	    return false;

	}
	bool help(string s1, string s2) {
		if (s1.size() != s2.size())return false;
		vector<int>test(128, 0);
		for (auto i : s1)test[i]++;
		for (auto i : s2)test[i]--;
		for (int i = 0; i < 128; ++i) { if (test[i])return false; }
		return true;
	}
};


88.合并两个排过序数组的前一段

取nums1的前m个以及nums2的前n个,组成一个新的序列到nums1再排序…
//并不想管时间复杂度直接用sort好了……

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        vector<int>result;
        result.insert(result.end(),nums1.begin(),nums1.begin()+m);
        result.insert(result.end(),nums2.begin(),nums2.begin()+n);
        sort(result.begin(),result.end());
        nums1=result;
        return;
    }
};


89.n位二进制数字的遍历

只允许0变成1或者1变成0,且只有一种顺序是允许的

For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:

00 - 0
01 - 1
11 - 3
10 - 2
n=3的时候只能是00 01 11 10 110 111 101 100这种……(限定死了,其实还有其他的方法


class Solution {
public:
    vector<int> grayCode(int n) {
        vector<int>result(pow(2,n),0);
        int temp=1;
        for(int i=0;i<n;++i){
            for(int j=0;j<temp;j++){
            result[temp+j]=temp+result[temp-j-1];
            }
            temp=temp<<1;
        }
        return result;
    }
};


90.有重复的子集


For example,
If nums = [1,2,2], a solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

是78题的延续,加一行sort以及一行find函数保证不重复就可以了……


class Solution {
public:
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
       sort(nums.begin(),nums.end());//防止出现414以及144两个重复的情况……
        vector<vector<int>>result;
        vector<int>temp;
        help(result,nums,temp,0);
        return result;
    }
    void help(vector<vector<int>>&result,vector<int>&nums,vector<int>temp,int begin){
        if(begin==nums.size()){
        if(find(result.begin(),result.end(),temp)==result.end())//这句话的主要作用防止重复,用到了STL的find函数
        result.push_back(temp);
            return;
        }
        temp.push_back(nums[begin]);help(result,nums,temp,begin+1);
        temp.pop_back();help(result,nums,temp,begin+1);
        return;
    }
};


要准备组会内容了,刷得眼睛有点酸。。。








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱铭德

五毛也是爱٩(●´৺`●)૭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值