1.实现 pow(x, n) ,即计算 x 的 n 次幂函数。
方法1 时间效率太差
class Solution {
public:
double quickMul(double x, long long N) {
double res[2]={1,x};
if(N<2)
return res[N];
double tem=x;
double res2;
for(long long i=1;i<N;i++)
{
res2=tem*x;
tem=res2;
}
return res2;
}
double myPow(double x, int n) {
if(x==0&&n<0)
return -1;
long long N = n;
return N >= 0 ? quickMul(x, N) : 1.0 / quickMul(x, -N);
}
};
方法2 迭代
class Solution {
public:
double myPow(double x, int n) {
if(n==0||x==1)
return 1;
if(n<0)
return 1/myPow1(x,abs(n));
return myPow1(x,n);
}
double myPow1(double x, uint32_t n) {
if(n==1)
return x;
if(n%2==1)
{
double res=myPow1(x,n/2);
return res*res*x;
}
else
{
double res=myPow1(x,n/2);
return res*res;
}
}
};
2.设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
push(x) —— 将元素 x 推入栈中。
pop() —— 删除栈顶的元素。
top() —— 获取栈顶元素。
getMin() —— 检索栈中的最小元素。
#include<stack>
#include<algorithm>
#include<limits.h>
class MinStack {
private:
stack<int> a;
stack<int> min1;
public:
/** initialize your data structure here. */
MinStack() {
min1.push(INT_MAX);
}
void push(int x) {
a.push(x);
min1.push(min(min1.top(),x));
}
void pop() {
a.pop();
min1.pop();
}
int top() {
return a.top();
}
int getMin() {
return min1.top();
}
};
3.二叉树的层序遍历
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector <vector <int>> ret;
if (!root) return ret;
queue <TreeNode*> q;
q.push(root);
while (!q.empty()) {
int currentLevelSize = q.size();
ret.push_back(vector <int> ());
for (int i = 1; i <= currentLevelSize; ++i) {
auto node = q.front(); q.pop();
ret.back().push_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
return ret;
}
};
4.只出现一次的数字
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
f1
int singleNumber(vector<int> nums) {
if (nums.empty())
return -1;
sort(nums.begin(), nums.end());
vector<int>::iterator iter;
for (iter = (nums.begin() + 1); iter != (nums.end() - 1); iter++)
{
if (*iter > *(iter - 1) && *iter < *(iter + 1))
return *iter;
}
int size = nums.size();
int sen = *(nums.begin() + 1);
int last2 = *(nums.begin() + size - 2);
if (nums.front() < sen)
return nums.front();
else if (nums.back() > last2)
return nums.back();
else
return -1;
}
f2
class Solution {
public int singleNumber(int[] nums) {
int single = 0;
for (int num : nums) {
single ^= num;
}
return single;
}
}
5.25. K 个一组翻转链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if(head==nullptr || k<=0)
return NULL;
ListNode *root=head;
int size=0;
while(root!=NULL)
{
size++;
root=root->next;
}
int n=size/k;
ListNode *first=head;
ListNode *second=dump_node(head,k);
ListNode* ne;
for(int a=1;a<=n;a++)
{
ne=Reverpart(head,second);
head=ne->next;
ListNode *second=dump_node(head,k);
}
return ne;
}
ListNode* dump_node(ListNode* node,int n)
{
ListNode* pnode=node;
for(int i=1;i<n;i++)
{
pnode=pnode->next;
}
return pnode;
}
ListNode* Reverpart(ListNode* node1,ListNode* node2)
{
ListNode* end=node2->next;
ListNode* pNode=node1;
ListNode* pPrev=end;
while(pNode!=end)
{
ListNode* pNext=pNode->next;
if(pNext==end)
end=pNode;
pNode->next=pPrev;
pPrev=pNode;
pNode=pNext;
}
return end;
}
};
6.1. 两数之和
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> twos;
int i;
int j;
int size=nums.size();
for(i=0;i<=size-1;i++)
{
for(j=i+1;j<=size-1;j++)
{
if(nums[i]+nums[j]==target)
{
twos.push_back(i);
twos.push_back(j);
}
}
}
return twos;
}
};
7,2. 两数相加
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* result1=nullptr;
if(l1==nullptr || l2==nullptr)
return result1;
static int pec=0;
ListNode* result=new ListNode();
ListNode* r= result;
while(l1 || l2 || pec!=0)
{
int sum=0;
if(l1)
{
sum=l1->val+sum;
l1=l1->next;
}
if(l2)
{
sum=l2->val+sum;
l2=l2->next;
}
if(!result->next)
{
sum=sum+pec;
pec=sum/10;
ListNode* tem=new ListNode();
result->next=tem;
tem->val=sum%10;
tem->next=nullptr;
result=tem;
}
}
return r->next;
}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode vHead(0), *p = &vHead;
int flag = 0;
while (l1 || l2 || flag) {
int tmp = 0;
if (l1 != nullptr) tmp += l1->val;
if (l2 != nullptr) tmp += l2->val;
tmp += flag;
flag = tmp / 10;
tmp %= 10;
ListNode *next = l1 ? l1 : l2;
if (next == nullptr) next = new ListNode(tmp);
next->val = tmp;
p->next = next;
p = p->next;
l1 = l1 ? l1->next : nullptr;
l2 = l2 ? l2->next : nullptr;
}
return vHead.next;
}
};
8. 3. 无重复字符的最长子串
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if (s.empty())
return 0;
vector<char> a;
int size = s.size();
int i=0;
int j=0;
int ma=0;
while (i < size && j<size)
{
a.push_back(s[j]);
if (count(a.begin(), a.end(), s[j]) == 1)
{
j++;
ma=ma>=a.size() ? ma : a.size();
}
else
{
a.erase(a.begin(),a.end());
i++;
j=i;
}
}
return ma;
}
};
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// 哈希集合,记录每个字符是否出现过
unordered_set<char> occ;
int n = s.size();
// 右指针,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动
int rk = -1, ans = 0;
// 枚举左指针的位置,初始值隐性地表示为 -1
for (int i = 0; i < n; ++i) {
if (i != 0) {
// 左指针向右移动一格,移除一个字符
occ.erase(s[i - 1]);
}
while (rk + 1 < n && !occ.count(s[rk + 1])) {
// 不断地移动右指针
occ.insert(s[rk + 1]);
++rk;
}
// 第 i 到 rk 个字符是一个极长的无重复字符子串
ans = max(ans, rk - i + 1);
}
return ans;
}
};
9. 7. 整数反转
class Solution {
public:
int reverse(int x) {
if (x == 0)
return 0;
int i = 0;
int a = x;
if (x < 0)
a = abs(x);
long b = 0;
while (a != 0)
{
b = (a % 10) + b * 10;
a = a / 10;
i++;
}
if (x <= 0)
b = -1 * b;
if (b > INT_MAX || b < INT_MIN)
return 0;
else
return b;
}
};
10.8. 字符串转换整数 (atoi)
class Solution {
public:
int myAtoi(string str) {
if (str.empty())
return 0;
long long result = 0;
int size = str.size();
bool a=false;
int i = 0;
while (str[i] == ' ')
i++;
if (str[i] == '+' && str[i + 1]<'0' && str[i + 1]>'9')
return 0;
if (str[i] == '+' && str[i + 1] >= '0' && str[i + 1] <= '9')
{
i++;
}
if (str[i] == '-' && str[i + 1]<'0' && str[i + 1]>'9')
return 0;
if (str[i] == '-' && str[i + 1] >= '0' && str[i + 1] <= '9')
{
a = true;
i++;
}
for (; i < size; i++)
{
if (str[i]>='0' && str[i]<='9')
{
if(result<=INT_MAX)
result = result * 10 + (str[i] - '0');
else
{
result= a ? INT_MIN:INT_MAX;
return result;
}
}
else
break;
}
if (a)
result = -1 * result;
if (result > INT_MAX )
return INT_MAX ;
if (result< INT_MIN )
return INT_MIN ;
return result;
}
};