招商银行信用卡
53. 最大子数组和
// shiran
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, n, a) for (int i = n - 1; i >= a; i--)
#define sz(x) (int)size(x)
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define pb push_back
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1000000007;
const int N = 1010;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
class Solution
{
public:
int maxSubArray(vector<int> &nums)
{
int a = nums[0], res = nums[0];
int n = sz(nums);
rep(i, 1, n)
a = max(nums[i], a + nums[i]),
res = max(res, a);
return res;
}
};
7. 整数反转
// shiran
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, n, a) for (int i = n - 1; i >= a; i--)
#define sz(x) (int)size(x)
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define pb push_back
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1000000007;
const int N = 1010;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
class Solution
{
public:
int reverse(int x)
{
ll t = x, ans = 0;
if (t < 0)
{
t = -t;
while (t)
{
ans = ans * 10 + t % 10;
t /= 10;
}
ans = -ans;
if (ans < INT_MIN)
return 0;
else
return ans;
}
else
{
while (t)
{
ans = ans * 10 + t % 10;
t /= 10;
}
if (ans > INT_MAX)
return 0;
return ans;
}
return 0;
}
};
33. 搜索旋转排序数组
先二分找到中间的分界点,然后再二分找到targettargettarget
// shiran
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, n, a) for (int i = n - 1; i >= a; i--)
#define sz(x) (int)size(x)
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define pb push_back
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1000000007;
const int N = 1010;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
class Solution
{
public:
int search(vector<int> &nums, int target)
{
int l = 0, r = nums.size() - 1;
while (l < r)
{
int mid = l + r + 1 >> 1;
if (nums[mid] >= nums[0])
l = mid;
else
r = mid - 1;
}
if (target >= nums[0])
l = 0;
else
l = r + 1, r = nums.size() - 1;
while (l < r)
{
int mid = l + r >> 1;
if (nums[mid] >= target)
r = mid;
else
l = mid + 1;
}
if (nums[r] == target)
return r;
else
return -1;
}
};