剑指offer 30 正则 旋转链表
股票
计算器
合法括号问题
最长重复子串,返回最长重复子串
根据nums[mid]<=nums[right])//右部分有序 然后分别判断left right改变
当元素相同时,2222212和212222222,比如else{
right--;
}
33搜索旋转排序数组
class Solution {
public:
int search(vector<int>& nums, int target) {
int left=0,right=nums.size()-1;
while(left<=right){
int mid = left +(right-left)/2;
if(target==nums[mid]){
return mid;
}
else if(nums[mid]<=nums[right]){//有部分有序
if(nums[mid]<=target && target <=nums[right]) left=mid+1;
else right=mid-1;
}else {//左部分有序
if(nums[mid]>=target && target >=nums[left]) right=mid-1;
else left=mid+1;
}
}
return -1;
}
};
81搜索旋转排序数组II
class Solution {
public:
bool search(vector<int>& nums, int target) {
int left=0,right=nums.size()-1;
while(left<=right){
int mid = left +(right -left)/2;
if(target == nums[mid]) return true;
if(nums[mid]<nums[right] || nums[mid]==nums[right] && nums[left]> nums[mid]){
if(target > nums[mid] && target <= nums[right]){
left=mid+1;
}else{
right=mid-1;
}
}else if(nums[mid]>nums[left] || nums[mid]==nums[left] && nums[right]< nums[mid]){
if(target < nums[mid] && target>= nums[left]){
right=mid-1;
}else{
left=mid+1;
}
}else{
right--;
}
}
return false;
}
};
left 和right 定位 旋转点位置 结束条件left(最大) right(最小)
剑指offer11旋转数组的最小数字(含重复数字)
class Solution {
public:
int minArray(vector<int>& numbers) {
int left=0,right=numbers.size()-1;
if(numbers[0]<numbers.back()) return numbers[0];
while(left+1<right){
int mid = left +(right-left)/2;
if(numbers[mid]<numbers[right]){
right=mid;
}else if(numbers[mid]>numbers[right]){
left=mid;
}else{
right--;
}
}
return numbers[right];
}
};
递归
class Solution {
public:
bool isMatch(string s, string p) {
if (p.empty()) return s.empty();
bool first_match = !s.empty() && (s[0] == p[0] || p[0] == '.');
// *前字符重复>=1次 || *前字符重复0次(不出现)
if (p.size() >= 2 && p[1] == '*')
return (first_match && isMatch(s.substr(1), p)) || isMatch(s, p.substr(2));
// 不是*,剪去已经匹配成功的头部,继续比较
else
return first_match && isMatch(s.substr(1), p.substr(1));
}
};
动态规划
剑指 Offer 14- I. 剪绳子
class Solution {
public:
int cuttingRope(int n) {
vector<int> dp(n+1,0);
dp[0]=dp[1]=1;
for(int i=2;i<=n;i++){
for(int j=1;j<i;j++){
dp[i]=max(dp[i],dp[i-j]*j);
dp[i]=max(dp[i],(i-j)*j);
}
}
return dp[n];
}
};
剑指 Offer 16. 数值的整数次方
注意溢出
class Solution {
public:
double myPow(double x, int n) {
if(n==0) return 1;
bool flag=false;
long N=n;
if(n<0){
flag=true;
N=-N;
}
return flag==true?1.0/help(x,N):help(x,N);
}
double help(double x, long n){
if(n==1) return x;
double y=help(x,n/2);
return n%2==0?y*y:x*y*y;
}
};
剑指 Offer 17. 打印从1到最大的n位数
class Solution {
public:
vector<int> printNumbers(int n) {
vector<int> ret;
if(n<=0) return ret;
for(int i=1;i<=9;i++){
ret.push_back(i);
}
int temp=9;
int num=pow(10,n)-1;
while(temp<num){
ret.push_back(++temp);
}
return ret;
}
};
剑指 Offer 18. 删除链表的节点
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
if(head==nullptr) return head;
if(val == head->val) return head->next;
ListNode* pre=head,*cur=head->next;
while(cur!=nullptr){
if(cur->val==val){
pre->next=cur->next;
break;
}
cur=cur->next;
pre=pre->next;
}
return head;
}
};
包含min函数的栈
class MinStack {
public:
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
s1.push(x);
if(s2.empty() || x<=s2.top()){
s2.push(x);
}
}
void pop() {
if(!s2.empty() && s1.top()==s2.top()){
s2.pop();
}
s1.pop();
}
int top() {
return s1.top();
}
int min() {
return s2.top();
}
private:
stack<int> s1;
stack<int> s2;
};
225. 用队列实现栈
class MyStack {
public:
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
int n=que.size();
que.push(x);
for(int i=0;i<n;i++){
int temp=que.front();
que.pop();
que.push(temp);
}
return;
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int temp=que.front();
que.pop();
return temp;
}
/** Get the top element. */
int top() {
return que.front();
}
/** Returns whether the stack is empty. */
bool empty() {
return que.empty();
}
private:
queue<int> que;
};