Format C++ codes in Clang-Format Style: Microsoft

This commit is contained in:
krahets
2023-04-14 03:44:02 +08:00
parent f8513455b5
commit 9c9c8b7574
46 changed files with 732 additions and 888 deletions
+11 -12
View File
@@ -7,17 +7,17 @@
#include "../include/include.hpp"
/* 二分查找(双闭区间) */
int binarySearch(vector<int>& nums, int target) {
int binarySearch(vector<int> &nums, int target) {
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
int i = 0, j = nums.size() - 1;
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
while (i <= j) {
int m = (i + j) / 2; // 计算中点索引 m
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
int m = (i + j) / 2; // 计算中点索引 m
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
j = m - 1;
else // 找到目标元素,返回其索引
else // 找到目标元素,返回其索引
return m;
}
// 未找到目标元素,返回 -1
@@ -25,29 +25,28 @@ int binarySearch(vector<int>& nums, int target) {
}
/* 二分查找(左闭右开) */
int binarySearch1(vector<int>& nums, int target) {
int binarySearch1(vector<int> &nums, int target) {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
int i = 0, j = nums.size();
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
while (i < j) {
int m = (i + j) / 2; // 计算中点索引 m
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中
int m = (i + j) / 2; // 计算中点索引 m
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
j = m;
else // 找到目标元素,返回其索引
else // 找到目标元素,返回其索引
return m;
}
// 未找到目标元素,返回 -1
return -1;
}
/* Driver Code */
int main() {
int target = 6;
vector<int> nums = { 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 };
vector<int> nums = {1, 3, 6, 8, 12, 15, 23, 67, 70, 92};
/* 二分查找(双闭区间) */
int index = binarySearch(nums, target);
cout << "目标元素 6 的索引 = " << index << endl;
@@ -55,6 +54,6 @@ int main() {
/* 二分查找(左闭右开) */
index = binarySearch1(nums, target);
cout << "目标元素 6 的索引 = " << index << endl;
return 0;
}
@@ -16,7 +16,7 @@ int hashingSearchArray(unordered_map<int, int> map, int target) {
}
/* 哈希查找(链表) */
ListNode* hashingSearchLinkedList(unordered_map<int, ListNode*> map, int target) {
ListNode *hashingSearchLinkedList(unordered_map<int, ListNode *> map, int target) {
// 哈希表的 key: 目标节点值,value: 节点对象
// 若哈希表中无此 key ,返回 nullptr
if (map.find(target) == map.end())
@@ -24,30 +24,29 @@ ListNode* hashingSearchLinkedList(unordered_map<int, ListNode*> map, int target)
return map[target];
}
/* Driver Code */
int main() {
int target = 3;
/* 哈希查找(数组) */
vector<int> nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
vector<int> nums = {1, 5, 3, 2, 4, 7, 5, 9, 10, 8};
// 初始化哈希表
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++) {
map[nums[i]] = i; // key: 元素,value: 索引
map[nums[i]] = i; // key: 元素,value: 索引
}
int index = hashingSearchArray(map, target);
cout << "目标元素 3 的索引 = " << index << endl;
/* 哈希查找(链表) */
ListNode* head = vecToLinkedList(nums);
ListNode *head = vecToLinkedList(nums);
// 初始化哈希表
unordered_map<int, ListNode*> map1;
unordered_map<int, ListNode *> map1;
while (head != nullptr) {
map1[head->val] = head; // key: 节点值,value: 节点
map1[head->val] = head; // key: 节点值,value: 节点
head = head->next;
}
ListNode* node = hashingSearchLinkedList(map1, target);
ListNode *node = hashingSearchLinkedList(map1, target);
cout << "目标节点值 3 的对应节点对象为 " << node << endl;
return 0;
@@ -7,7 +7,7 @@
#include "../include/include.hpp"
/* 线性查找(数组) */
int linearSearchArray(vector<int>& nums, int target) {
int linearSearchArray(vector<int> &nums, int target) {
// 遍历数组
for (int i = 0; i < nums.size(); i++) {
// 找到目标元素,返回其索引
@@ -19,7 +19,7 @@ int linearSearchArray(vector<int>& nums, int target) {
}
/* 线性查找(链表) */
ListNode* linearSearchLinkedList(ListNode* head, int target) {
ListNode *linearSearchLinkedList(ListNode *head, int target) {
// 遍历链表
while (head != nullptr) {
// 找到目标节点,返回之
@@ -31,20 +31,19 @@ ListNode* linearSearchLinkedList(ListNode* head, int target) {
return nullptr;
}
/* Driver Code */
int main() {
int target = 3;
/* 在数组中执行线性查找 */
vector<int> nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
vector<int> nums = {1, 5, 3, 2, 4, 7, 5, 9, 10, 8};
int index = linearSearchArray(nums, target);
cout << "目标元素 3 的索引 = " << index << endl;
/* 在链表中执行线性查找 */
ListNode* head = vecToLinkedList(nums);
ListNode* node = linearSearchLinkedList(head, target);
ListNode *head = vecToLinkedList(nums);
ListNode *node = linearSearchLinkedList(head, target);
cout << "目标节点值 3 的对应节点对象为 " << node << endl;
return 0;
}