使用工具:xcode
题目描述:
141. Linked List Cycle
不用额外空间判断链表是否存在环
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head == NULL) return 0;
ListNode *p1 = head;
ListNode *p2 = head->next;
while(p1 && p2) {
if(p1!=p2) {
if(p2->next) {
p1 = p1->next;
p2 = p2->next->next;
} else
return 0;
} else
return 1; //快慢指针,有环 }
return 0;
}
};
142. Linked List Cycle II
如果有环,返回环的第一个指针,否则返回null,快慢指针(不能有额外的空间)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(head == NULL) return head;
ListNode *p1 = head;
ListNode *p2 = head->next;
while(p1 && p2 && p1!=p2) {
if(p2->next == NULL)
return NULL;
p1 = p1->next;
p2 = p2->next->next;
}
if(p1==NULL || p2==NULL) return NULL;
//A--B--C:分别为起始点,环起点,相遇到点 //(AB+BC+mL)*2 = AB+BC+nL-->AB+BC = kL //即从C点再走AB那么远可以到达圆环起点 p1 = head;
p2 = p2->next; //注意这里 while(p1!=p2) {
p1 = p1->next;
p2 = p2->next;
}
return p1;
}
};
84. Largest Rectangle in Histogram
class Solution {
public:
int largestRectangleArea(vector& heights) {
stack temp;
int large = 0;
for(int i = 0; i
if(i==0 || heights[i]>=temp.top())
temp.push(heights[i]);
else {
int k = 0; // 用于计数 int high = 0;
while (temp.size()>0 && heights[i]
k++;
high = temp.top();
temp.pop();
large = large>(high*k)?large:high*k;
}
while (k>=0) {
temp.push(heights[i]);
k--;
}
}
}
int size = (int)temp.size();
int high = 0;
for(int i=1; i<=size; i++) {
high = temp.top();
temp.pop();
large = large>high*i?large:high*i;
}
return large;
}
};
461. Hamming Distance
两个int型的汉明码距(转换为二进制后对应位置字符不同的位数)
class Solution {
public:
int hammingDistance(int x, int y) {
int temp = x^y; //x与y异或 int n = 0; //汉明码距 for(int i=0; i<32; i++) {
if(temp%2==1)
n++;
temp>>=1;
}
return n;
}
};
191. Number of 1 Bits
无符号int汉明权重
class Solution {
public:
int hammingWeight(uint32_t n) {
int m = 0; //汉明码距 for(int i=0; i<32; i++) {
if(n%2==1)
m++;
n>>=1;
}
return m;
}
};
190. Reverse Bits
无符号数二进制翻转
class Solution {
public:
//交换无符号数的第a位与第b位,a、b介于0……31 void exchangBits(uint32_t &n, int a, int b) {
int m1 = (n&(1<>a;
int m2 = (n&(1<>b;
if(m1==m2)
return;
else if(m1==1)
n=n-(1<
else
n = n-(1<
}
uint32_t reverseBits(uint32_t n) {
for(int i=0; i<=15; i++)
exchangBits(n, i, 31-i);
return n;
}
};
7. Reverse Integer
int位翻转
class Solution {
public:
int reverse(int x) {
if(x/10 == 0) return x;
bool flag = x>0;
x = abs(x); //x的绝对值 int result = 0;
while(x>0) {
if(result>INT_MAX/10 || result
result = result*10 + x%10; //依次处理个位、十位、百位……这里的思路参考了网上的博客 x = x/10;
}
if(!flag)
result = -result;
return result;
}
};
111. Minimum Depth of Binary Tree
二叉树的最小深度
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) { //利用广度优先遍历 if(root==NULL) return 0;
int depth = 1; //深度 int n1 = 1, n2 = 1, p = 0; //目前遍历的结点p,到目前层一共的结点数n1,到下一层的总数n2 queue q;
q.push(root);
TreeNode* pp = root;
while(pp->left!=NULL || pp->right!=NULL) {
q.pop();
p++;
if(pp->left!=NULL) {
q.push(pp->left);
n2++;
}
if(pp->right!=NULL) {
q.push(pp->right);
n2++;
}
pp = q.front();
if(p==n1) {
depth++;
n1 = n2;
}
}
return depth;
}
};
104. Maximum Depth of Binary Tree
二叉树的最大深度
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==NULL) return 0;
int depth = 1;
int n1 = 1, n2 = 1, p = 0; //目前遍历的结点p,到目前层一共的结点数n1,到下一层的总数n2 queue q;
q.push(root);
TreeNode* pp = root;
while(!q.empty()) {
pp = q.front();
q.pop();
p++;
if(p>n1) {
depth++;
n1 = n2;
}
if(pp->left!=NULL) {
q.push(pp->left);
n2++;
}
if(pp->right!=NULL) {
q.push(pp->right);
n2++;
}
}
return depth;
}
};
110. Balanced Binary Tree
二叉树是否为平衡二叉树
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int getHige(TreeNode* root, bool &flag) {
int a = 0, b = 0;
if(root==NULL) return 0;
if(root->left && flag)
a = getHige(root->left, flag);
if(root->right && flag)
b = getHige(root->right, flag);
if((b-a)>1 || (a-b)>1)
flag = false;
return (a>b?a:b)+1;
}
bool isBalanced(TreeNode* root) {
if(root==NULL) return true;
bool flag = true;
int h1 = 0, h2 = 0;
if(root->left)
h1 = getHige(root->left, flag);
if(root->right)
h2 = getHige(root->right, flag);
if((h1-h2)>1 || (h2-h1)>1)
flag = false;
return flag;
}
};