二进制中1的个数、数值的整数次方、调整数组顺序使奇数位于偶数前面、链表中倒数k个结点(剑指offer11-14)c++版

代码可以跑通,转载请注明出处。

#include 
#include 
#include 

using namespace std;

struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
		val(x), next(NULL) {
	}
};

void test1();//JZ11
void test2();//JZ12
void test3();//JZ13
void test4();//JZ14

class Solution {
public:
	int MAX = 0;
	//JZ11 二进制中1的个数
	int NumberOf1(int n);
	int NumberOf1_1(int n);
	//JZ12 数值的整数次方
	double Power(double base, int exponent);
	double PowerCore(double base, int exponent);
	//JZ13 调整数组顺序使奇数位与偶数前面
	void reOrderArray(vector<int> &array);
	//JZ14 链表中倒数K个结点
	ListNode* FindKthToTail(ListNode* pListHead, unsigned int k);
};

int Solution::NumberOf1(int n) {
	//按位与,同为1为1,不同为0
	//初始将n与1位与运算,若非0,则表明n的最后一位为1;再将n左移一位,再判断。
	int result = 0;
	int temp = 1;
	for (int i = 0; i <= 32; i++) {
		if (n & temp)	result++;
		temp = temp << 1;
	}
	return result;
}
int Solution::NumberOf1_1(int n) {
	int count = 0;
	while (n) {
		n = n & (n - 1);
		count++;
	}
	return count;
}
void Solution::reOrderArray(vector<int> &array) {
	//冒泡排序算法
	if (array.empty())	return;
	int len = array.size();
	for (int i = len - 1; i >= 0; i--) {
		for (int j = 0; j < i; j++) {
			if (array[j] % 2 == 0)	
				if (array[j+1] % 2 == 1)
					swap(array[j], array[j + 1]);
		}
	}
	return;
}
double Solution::Power(double base, int exponent) {
	if (base == 0 && exponent <= 0) {
		MAX = -1;
		return (double)0;
	}
	if (exponent == 0)	return 1;
	double result = 0;
	if (exponent > 0)	result = PowerCore(base, exponent);
	else
	{
		result = 1 / PowerCore(base, -exponent);
	}
	return result;
}
double Solution::PowerCore(double base, int exponent) {
	double result = 0;
	if (exponent == 0)	return 1;
	if (exponent % 2 == 0) {
		double temp = PowerCore(base, exponent / 2);
		result = temp * temp;
	}
	else {
		double temp = PowerCore(base, (exponent - 1) / 2);
		result = temp * temp * base;
	}
	if (result == DBL_MAX)	MAX = -2;//溢出
	return result;
}
ListNode* Solution::FindKthToTail(ListNode* pListHead, unsigned int k) {
	//只遍历一遍。快慢指针,快指针先前进k步,慢指针开始前进,当快指针遇到链表的尾结点的时候,慢指针指向
	//倒数第k个结点
	if (!pListHead || k <= 0)	return nullptr;
	ListNode* slow = pListHead;
	ListNode* fast = pListHead;
	int count = 1;
	while (count < k && fast->next) {
		fast = fast->next;
		count++;
	}
	if (count == k - 1)	return nullptr;
	while (fast->next) {
		slow = slow->next;
		fast = fast->next;
	}
	return slow;
}

void test1() {
	int n = -32;
	Solution s;
	cout << s.NumberOf1_1(n);
	return;
}
void test2() {
	Solution s;
	s.Power(2, 1);
	return;
}
void test3() {
	vector<int> test = { 1,2,3,4,5,7 };
	Solution s;
	int len = test.size();
	int i = 0;
	s.reOrderArray(test);
	while (i < len) {
		cout << test[i] << endl;
		i++;
	}
	return;
}
void test4() {
	Solution s;
	ListNode* pHead = new ListNode(0);
	pHead->next = new ListNode(1);
	pHead->next->next = new ListNode(2);
	pHead->next->next->next = new ListNode(3);
	ListNode* result = s.FindKthToTail(pHead, 0);
	cout << result->val;
	return;
}

int main() {
	//test1();
	//test2();
	//test3();
	test4();
	system("pause");
	return 0;
}

你可能感兴趣的:(剑指offer,算法,算法)