问题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的连续整数序列

``````#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;
}
};``````