【LeetCode81-90】三道链表,两道找最大面积的hard题,一道二叉树的hard题和一些找自信题……
来源:互联网 发布:21cn企业邮箱域名 编辑:程序博客网 时间:2024/06/05 09:53
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 01 0 1 1 11 1 1 1 11 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.合并两个排过序数组的前一段
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 - 001 - 111 - 310 - 2n=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; }};
要准备组会内容了,刷得眼睛有点酸。。。
- 【LeetCode81-90】三道链表,两道找最大面积的hard题,一道二叉树的hard题和一些找自信题……
- 【LeetCode111-120】很多道二叉树的问题(左右顺序很重要),一道hard DP题,两道杨辉三角以及一道DP三角
- 一道找规律的题
- 一道小米的关于柱状图求最大矩阵面积的题
- HDU 5858 Hard problem【两圆的面积交】
- 找一个二叉树的最大路径和
- Hard 找最小的k个数(Selection Rank) @CareerCup
- Hard 57题 Insert Interval
- Hard 56题 Merge Intervals
- Hard 146题 LRU Cache
- Hard 68题 Text Justification
- Hard 65题 Valid Number
- leetcode上面难度hard题
- hdu Hard challenge (几何题)
- 一道网上找的微软笔试题
- 一道狐狸找兔子的题
- c::一道关于找凶手的题
- 二叉查找树的一道题
- 腾讯笔试编程题
- A Bug's Life
- HUD 2094(set简单应用)
- android MediaRecorder实现录音功能 显示时间 计时
- 递归
- 【LeetCode81-90】三道链表,两道找最大面积的hard题,一道二叉树的hard题和一些找自信题……
- 改进后的扫雷
- 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
- 第一次博客学习笔记
- 跨域sso实现
- 设计模式-2-抽象工厂方法
- 有关Linux内核分析课程的总结
- 百度地图根据省市县定位,经纬度和地址信息互相转化JS示例
- 【阿里云服务器】在Centos6.5上安装与配置MySQL