问题01 数字在排序数字中的数量
最笨的方法使用Map统计遍历,但是题目是有序数组,所以必然是二分查找,可以去寻找key的上一个元素和下一个元素,时间都是O(lon(n)),然后计算范围即可。
问题02 二叉树的深度
这个实在是太简单了,直接DFS深度优先遍历即可
问题03 数组中只出现一次的数字
- 先考虑一个数组里只有一个数出现一次,其他两个数都出现两次的情况:一个数跟自己异或后为0,一个数组里只有一个数出现一次其他两次,挨个异或后最后得到的结果就是只出现一次的那个数。
//num1,num2分别为长度为1的数组。传出参数
//将num1[0],num2[0]设置为返回结果
public class Solution {
public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
if (array == null || array.length == 0) {
return;
}
if (array.length < 2) {
num1[0] = 0;
num2[0] = 0;
return;
}
int temp1 = array[0];
for (int i = 1; i < array.length; i++) {
temp1 = temp1 ^ array[i];
}
int firstBit = 0;
while (((temp1 & 1) == 0) && (firstBit < 32)) {
firstBit++;
temp1 = temp1 >> 1;
}
num1[0] = 0;
num2[0] = 0;
for (int i = 0; i < array.length; i++) {
if (isBit(array[i], firstBit)) {
num1[0] = num1[0] ^ array[i];
} else {
num2[0] = num2[0] ^ array[i];
}
}
return;
}
public boolean isBit(int num, int indexBit) {
num = num >> indexBit;
return (num & 1) == 1;
}
}
问题04 和为s的两个数字VS和为s的连续整数序列
对于上题,是指left和right指针,双指针遍历判断即可,千万不要暴力求解。
分析如下:
代码如下:
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>
#include <numeric>
#include <cmath>
#include <regex>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
class Solution
{
public:
vector<int> getOne(int begin, int end)
{
vector<int> one;
for (int i = begin; i <= end; i++)
one.push_back(i);
return one;
}
vector<vector<int> > FindContinuousSequence(int sum)
{
vector<vector<int> > res;
if (sum < 1 + 2)
return res;
int left = 1, right = 2, mid = (1 + sum) / 2;
int curSum = left + right;
while (left < mid)
{
if (curSum == sum)
res.push_back(getOne(left, right));
else if (curSum > sum)
{
while (curSum > sum && left < mid)
{
curSum -= left;
left++;
}
if (curSum == sum)
res.push_back(getOne(left, right));
}
right++;
curSum += right;
}
return res;
}
};
问题05 翻转单词顺序 VS 左旋转字符串
直接使用C++的stringstream来做,很方便,或者遍历空格来翻转
直接旋转吧,
其实字符串处理对于C++和Java来说还是很容易去做的,注意考虑各种极端情况即可。