class Solution {
public:
int Fibonacci(int n) {
int i=2;
int prev=0,cur=1;
if(n==0)
return 0;
if(n==1)
return 1;
while(i++<=n)
{
int tmp=cur;
cur=cur+prev;
prev=tmp;
}
return cur;
}
};
class Solution1 {
public:
int NumberOf1(int n) {
int i=0;
int tag=1;
int count=0;
while(i++<32){
if((n&(tag))!=0)
count++;
tag=tag<<1;
}
return count;
}
};
//最优解
class Solution {
public:
int NumberOf1(int n) {
int i=0;
while(n){
n=(n-1)&n;
i++;
}
return i;
}
};
class Solution {
public:
int invalidval = 0;
double Power(double base, int exponent) {
double result = 0;
if (Isequel(base, 0.0) && exponent<0)
{
return 0;
invalidval = 1;
}
if (exponent<0)
{
result = UnPower(base, (unsigned int)(-exponent));
result = 1 / result;
return result;
}
result = UnPower(base, (unsigned int)(exponent));
return result;
}
double UnPower(double base, unsigned int exp)
{
double result = base;
if (exp == 0)
return 1;
if (exp == 1)
return base;
result = UnPower(base, exp >> 1);
result *= result;
if ((exp & 1) == 1)
result *= base;
return result;
}
int Isequel(double num1, double num2)
{
if ((num1 - num2)>-0.0000001 && (num1 - num2)<0.0000001)
return 1;
return 0;
}
};
int Numadd(char* num, int bit)
{
int i = bit,needcarry=1;
char carry = 1;
while (i >0)
{
num[i-1]+=carry ;
num[i] ='0';
if (num[i - 1] <= '9')
{
needcarry = 0;
break;
}
--i;
}
if (needcarry == 1)
{
num[0] = '1';
num[bit + 1] = '0';
}
return needcarry;
}
void Print1toN(int n)
{
if (n <= 0)
return;
char num[MAX_SIZE] = {0};
int i = 0;
while (1)
{
if (num[i]<'0')
num[i] = '0';
if (++num[i]>'9')
{
if (Numadd(num, i) == 1)
++i;
}
if (i == n)
break;
cout << num << ' ';
//_sleep(100);
}
cout << endl;
}
class Solution2 {
public:
int minNumberInRotateArray(vector<int> rotateArray) {
if (rotateArray.size() == 0)
{
i = 1;//类成员变量判断 输入参数是否合理
return 0;
}
if (rotateArray.size() == 1)
return rotateArray[0];
int head = 0, end = rotateArray.size() - 1;
int index = MinNumer(rotateArray, head, end);
return index;
}
private:
int i = 0;
int MinNumer(vector<int> rotateArray, int head, int end)
{
if ((end - head) == 1)
return rotateArray[end]<rotateArray[head] ? rotateArray[end] : rotateArray[head];
int middel = (end + head) / 2;
int index = 0;
if (rotateArray[head] == rotateArray[middel] && \
rotateArray[head] == rotateArray[end])//如果是相同数中夹杂最小数无法用二分 用顺序方法
{
for (int i = 0; i<(int)rotateArray.size(); ++i)
{
if (rotateArray[i]<rotateArray[i - 1])
return rotateArray[i];
}
}
else{
if (rotateArray[head] >rotateArray[middel] || rotateArray[middel] <= rotateArray[end])//中间数小于最左数或者 小于等于最右数 选左区间
index = MinNumer(rotateArray, head, middel);
else
index = MinNumer(rotateArray, middel, end);
}
return index;
}
};
附加条件:操作过后 不改变奇数之间与偶数之间的相对位置
类似于前后指针 实现快速排序的方法
考虑可扩展性 :可将 条件判断封装为函数
class Solution1 {
public:
void reOrderArray(vector<int> &array) {
int prev = 0, cur = 0;
while (cur<(int)array.size())
{
if ((array[cur] & 1) == 1)
{
int i = cur;
while (prev < i)
{
if ((array[i] & 1) == 1 && (array[i -1] & 1) == 0)
{
int tmp = array[i];
array[i] = array[i - 1];
array[i - 1] = tmp;
}
i--;
}
prev++;
}
cur++;
}
}
};
//替换的思想:可以把删除节点next中的内容复制到 该节点 删除下一个 时间复杂度O(1);考虑只有一个节点 与删除尾节点
void DeleteNode(ListNode** head,ListNode* del)
{
if(*head==NULL||del==NULL)
return;
ListNode* tmp=NULL;
if(del->next==NULL)
{
if(del==*head)
{
free(*head);
*head=NULL;
}
else
{
tmp=*head;
while(tmp->m_pNext!=del)
{
tmp=tmp->m_pNext;
}
tmp->m_pNext=NULL;
}
}
else
{
tmp=del->m_pNext;
del->m_nValue=tmp->m_nValue;
del->m_pNext=tmp->m_pNext;
}
free(tmp);
}
转载于:https://blog.51cto.com/shaungqiran/1771580