diff --git a/Week_01/MoveZero.cpp b/Week_01/MoveZero.cpp new file mode 100644 index 0000000..460d390 --- /dev/null +++ b/Week_01/MoveZero.cpp @@ -0,0 +1,16 @@ +#include + +/*******移动零 +* 双指针 +*/ + + +void moveZeroes(std::vector& nums) { + //双指针,一个指针记录0位置,一个用来遍历 + int j = 0; + for (int i = 0; i < nums.size(); i++) { + if (nums[i] != 0) { + std::swap(nums[j++], nums[i]); + } + } +} \ No newline at end of file diff --git a/Week_01/RemoveDuplicates.cpp b/Week_01/RemoveDuplicates.cpp new file mode 100644 index 0000000..b33a2c5 --- /dev/null +++ b/Week_01/RemoveDuplicates.cpp @@ -0,0 +1,20 @@ +#include + +/*********删除排序数组中的重复项 +* +* 解法:双指针,时间复杂度O(n),空间复杂度O(1) +* 第一遍做时,知道用双指针,但是判断出重复元素之后不知道如何去重,还整了个嵌套循环。。。 +*/ + +int removeDuplicates(std::vector& nums) +{ + if (nums.size() <= 1) return nums.size(); + + int j = 0; + for (int i = 1; i < nums.size(); i++){ + if (nums[i] != nums[j]) { + nums[++j] = nums[i];//直接覆盖掉就好了,就相当于统一后移了,不需要交换元素 + } + } + return j + 1; +} \ No newline at end of file diff --git a/Week_01/Rotate.cpp b/Week_01/Rotate.cpp new file mode 100644 index 0000000..1d4a041 --- /dev/null +++ b/Week_01/Rotate.cpp @@ -0,0 +1,23 @@ +#include +//旋转数组 + +/*解法: +* 1、新建数组,挨个遍历,时间复杂度O(n),空间复杂度O(1)——不满足要求,要求原地翻转 +* 2、暴力解法 +* 3、分段反转,时间复杂度O(n) +* 4、环形链接——没看懂 +*/ + +void rotate(std::vector& nums, int k) { + if (nums.size() < k) k %= nums.size();//刚开始没有考虑这种情况 + std::reverse(nums.begin(), nums.end()); + reverse(nums, 0, k - 1); + reverse(nums, k, nums.size() - 1); +} + +void reverse(std::vector& nums, int nStart, int nEnd) { + int j = nEnd; + for (int i = nStart; i <= j; i++) { + std::swap(nums[i], nums[j--]); + } +} \ No newline at end of file diff --git a/Week_02/GroupAnagrams.cpp b/Week_02/GroupAnagrams.cpp new file mode 100644 index 0000000..76f4ad7 --- /dev/null +++ b/Week_02/GroupAnagrams.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +/* 字母异位词 +* 哈希表——时空复杂度O(nklogk)——n为字符串数目,k为字符串最大长度 +* 先排序,存到哈希表中 +*/ + +using namespace std; +vector> groupAnagrams(vector& strs) { + vector> result; + unordered_map> mapGroup; + for (int i = 0; i < strs.size(); i++) { + string str = strs[i]; + sort(str.begin(), str.end()); + mapGroup[str].push_back(strs[i]); + } + + auto ito = mapGroup.begin(); + while (ito != mapGroup.end()){ + result.push_back(ito->second); + ito++; + } + + return result; +} \ No newline at end of file diff --git a/Week_02/InorderTraversal.cpp b/Week_02/InorderTraversal.cpp new file mode 100644 index 0000000..6acd498 --- /dev/null +++ b/Week_02/InorderTraversal.cpp @@ -0,0 +1,39 @@ +#include +#include + +/*二叉树的中序遍历 +* 解法 +* 1、递归——时间复杂度O(n),空间复杂度O(1) +* 2、迭代——时间复杂度O(n),空间复杂度O(n)——我感觉迭代用的时机就是防止递归太深造成堆栈溢出,否则用递归就好,简单易懂 +* 2.1、栈 +*/ +using namespace std; + +struct TreeNode { + int val; + TreeNode* left; + TreeNode* right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {} +}; + +vector inorderTraversal(TreeNode* root) { + vector result; + stack st; + if (root == nullptr) return result; + while (!st.empty() || root) {//注意这个根节点的放入时机,判断条件已经多次写错了 + //遍历所有左子节点 + while (root) { + st.push(root); + root = root->left; + } + //左子节点值 + root = st.top(); + st.pop(); + result.push_back(root->val); + root = root->right; + } + return result; +} + diff --git a/Week_02/LevelOrderN.cpp b/Week_02/LevelOrderN.cpp new file mode 100644 index 0000000..6b7ec2a --- /dev/null +++ b/Week_02/LevelOrderN.cpp @@ -0,0 +1,64 @@ +#include +#include + +using namespace std; + +class Node { +public: + int val; + vector children; + + Node() {} + + Node(int _val) { + val = _val; + } + + Node(int _val, vector _children) { + val = _val; + children = _children; + } +}; + +/*N叉树层序遍历 +1、队列——时间复杂度O(n),空间复杂度O(n) +1.1、队列的值:que(1)/pop(1),que()/que(3,2,4)/pop(3),que(2,4)/que(2,4,5,6)/pop(2),que(4,5,6)/pop(4),que(5,6)/pop(5),que(6)/pop(6),que() +2、广度优先遍历——待做 +3、深度优先遍历——待做 +*/ +vector> levelOrder(Node* root) { + queue que; + vector> result; + if (root != nullptr) que.push(root); + while (!que.empty()) { + int size = que.size(); + vector levelResult; + for (int i = 0; i < size; i++) {//注意size不能写成que.size(),因为que一直在变 + Node* p = que.front(); + que.pop(); + levelResult.push_back(p->val); + for (int j = 0; j < p->children.size(); j++) { + que.push(p->children[j]); + } + } + result.push_back(levelResult); + } + return result; +} + +int main() +{ + //1, null, 3, 2, 4, null, 5, 6 + Node* c11 = new Node(5); + Node* c12 = new Node(6); + vector vecNode = { c11, c12 }; + + Node* c1 = new Node(3, vecNode); + Node* c2 = new Node(2); + Node* c3 = new Node(4); + + vector vecNodeRoot = { c1,c2,c3 }; + Node* root = new Node(1, vecNodeRoot); + //preorder(root); + levelOrder(root); +} \ No newline at end of file diff --git a/Week_02/PreorderN.cpp b/Week_02/PreorderN.cpp new file mode 100644 index 0000000..6882ccd --- /dev/null +++ b/Week_02/PreorderN.cpp @@ -0,0 +1,67 @@ +#include +#include +#include + +using namespace std; + +class Node { +public: + int val; + vector children; + + Node() {} + + Node(int _val) { + val = _val; + } + + Node(int _val, vector _children) { + val = _val; + children = _children; + } +}; + +/*二叉树前序遍历 +* 解法: +* 1、递归——时间复杂度O(n),空间复杂度O(1)——输出结果占用空间不计入 +* 2、迭代——时间复杂度O(n),空间复杂度O(n) +*/ +vector preorder(Node* root) { + vector result; + if (root == nullptr) return result; + //traverse(root, result); + + //迭代法 + stack st; + st.push(root); + int childSize = 0; + while (!st.empty()) { + root = st.top(); + if (root == nullptr) continue; + st.pop(); + result.push_back(root->val); + childSize = root->children.size(); + if(childSize == 0) continue; + for(int i = childSize - 1; i >= 0; i--){ + if(root->children[i]) st.push(root->children[i]); + } + root = root->children[0]; + } + return result; +} + +int main() +{ + //1, null, 3, 2, 4, null, 5, 6 + Node* c11 = new Node(5); + Node* c12 = new Node(6); + vector vecNode = {c11, c12}; + + Node* c1 = new Node(3,vecNode); + Node* c2 = new Node(2); + Node* c3 = new Node(4); + + vector vecNodeRoot = {c1,c2,c3}; + Node* root = new Node(1, vecNodeRoot); + preorder(root); +} \ No newline at end of file diff --git a/Week_02/TopK.cpp b/Week_02/TopK.cpp new file mode 100644 index 0000000..741a2bd --- /dev/null +++ b/Week_02/TopK.cpp @@ -0,0 +1,49 @@ +#include +#include +#include + +/*最高频率的K个数 +* 优先队列——时间复杂度O(nlog(k)) +*/ + +using namespace std; +static bool cmp(pair& m, pair& n) { + return m.second < n.second; +} + +vector topKFrequent(vector& nums, int k) { + vector result; + if (nums.size() < k) return result; + unordered_map hashmap; + for (int i = 0; i < nums.size(); i++) hashmap[nums[i]]++; + + priority_queue, vector>, decltype(&cmp)> pq(cmp); + auto ito = hashmap.begin(); + while (ito != hashmap.end()) { + //这里可以做一些优化 + if (pq.size() == k) { + if (pq.top().second < ito->second) { + pq.pop(); + pq.emplace(ito->first, ito->second); + } + } + else { + pq.push(std::make_pair(ito->first, ito->second)); + } + ito++; + } + + for (int i = 0; i < k; i++) { + if (pq.size() == 0) break; + result.push_back(pq.top().first); + pq.pop(); + } + return result; +} + +int main() +{ + vector nums = { 1,1,1,2,2,3 }; + int k = 2; + topKFrequent(nums, k); +} \ No newline at end of file diff --git a/Week_03/Combine.cpp b/Week_03/Combine.cpp new file mode 100644 index 0000000..b0288cf --- /dev/null +++ b/Week_03/Combine.cpp @@ -0,0 +1,31 @@ +#include +/* +解法:递归 +优化的地方:尽量减少值拷贝,否则内存消耗大,性能也慢。 +*/ +using namespace std; + +vector single; +vector> result; +void traverse(int i, int n, int k) { + if (single.size() == k) { + result.push_back(single); + return; + } + if ((k - single.size()) > (n - i + 1)) return;//较少无用的判断,否则超出时间限制 + + traverse(i + 1, n, k); + single.push_back(i); + traverse(i + 1, n, k); + single.pop_back();//single做成全局变量,如果作为参数传递,会产生值传递,性能慢,内存高 +} + +vector> combine(int n, int k) { + traverse(1, n, k); + return result; +} + +void main() +{ + combine(20, 16); +} \ No newline at end of file diff --git a/Week_03/GenerateParenthesis.cpp b/Week_03/GenerateParenthesis.cpp new file mode 100644 index 0000000..abff00b --- /dev/null +++ b/Week_03/GenerateParenthesis.cpp @@ -0,0 +1,25 @@ +#include +#include + +using namespace std; + +void dfs(vector& res, string path, int n, int lc, int rc) { + if (rc > lc || lc > n || rc > n) return; + if (lc == rc && lc == n) { + res.push_back(path); + return; + } + dfs(res, path + '(', n, lc + 1, rc); + dfs(res, path + ')', n, lc, rc + 1); +} +vector generateParenthesis(int n) { + vector res; + int lc = 0, rc = 0; + dfs(res, "", n, lc, rc); + return res; +} + +//int main() +//{ +// generateParenthesis(3); +//} \ No newline at end of file diff --git a/Week_03/Permute.cpp b/Week_03/Permute.cpp new file mode 100644 index 0000000..b9a05ff --- /dev/null +++ b/Week_03/Permute.cpp @@ -0,0 +1,25 @@ +#include +/*回溯法 +还是要再练习 +时间复杂度O(n*n!),空间复杂度O(n)*/ + +using namespace std; +vector> result; +void dfs(int cur, vector& nums) { + if (cur == (nums.size() - 1)) { + result.push_back(nums); + return; + } + + for (int i = cur; i < nums.size(); ++i) {//注意这里是++i,这样就不用再写单独判断的函数了 + swap(nums[cur], nums[i]); + dfs(cur + 1, nums); + swap(nums[i], nums[cur]);//回溯法一定要把状态回置 + } +} + +vector> permute(vector& nums) { + result.clear(); + dfs(0, nums); + return result; +} \ No newline at end of file diff --git a/Week_03/README.md b/Week_03/README.md index 50de304..74edc1e 100644 --- a/Week_03/README.md +++ b/Week_03/README.md @@ -1 +1,10 @@ -瀛︿範绗旇 \ No newline at end of file +瀛︿範绗旇 +杩欏懆涓昏缁冧範鐨勬槸閫掑綊鍜屽洖婧備箣鍓嶄互涓鸿嚜宸遍掑綊寰堢啛鎮夛紝浣嗘槸缁冧範鐨勬椂鍊欐墠鍙戠幇鑷繁鐨勯棶棰 +1锛夌粡甯稿鍊间紶閫掑拰鍧浼犻掑紕涓嶆竻妤氾紝瀵艰嚧缁撴灉缁忓父涓嶅 +2锛夊洖婧椂涓嶇煡閬撴妸鐘舵佸洖缃 +3锛夊洖婧侀掑綊鐨勭┖闂村鏉傚害鍜屾椂闂村鏉傚害杩樿鏍煎娉ㄦ剰璁$畻 +3锛夊悗闈㈣繕瑕侀獙璇侀掑綊瑙f硶浠ュ強鍥炴函瑙f硶鑳藉惁鍐欐垚杩唬鏂瑰紡 + +杩樻槸瑕佺粌涔犲洖婧鐩 + +杩樻湁灏辨槸杩欏懆缁冧範棰戠巼涓嶅锛屼笅鍛ㄤ竴瀹氳鍧氭寔姣忓ぉ鐨勪緥棰樹互鍙婃瘡鏃ヤ竴棰樺綋澶╁仛瀹屻備簲姣掓硶涓鐩存病鏈変弗鏍兼墽琛岋紝閲嶅仛澶嶄範棰戠巼涓嶅 \ No newline at end of file diff --git a/Week_03/buildTree.cpp b/Week_03/buildTree.cpp new file mode 100644 index 0000000..0549e6b --- /dev/null +++ b/Week_03/buildTree.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} + }; + + + /* + 自己手动可以画出来,但是代码没思路 + */ + +unordered_map mapIn; +//nL:左边界,nR:右边界,nPreIndex:前序遍历的索引 +TreeNode* rebuildTree(vector& preorder, vector& inorder, int nL, int nR, int& nPreIndex) { + //这里nPreIndex写错了,所有结点遍历一遍,这里一定要是址传递,不能是值传递。否则每一层循环结束后还会重新遍历。 + if (nL > nR || nPreIndex >= preorder.size()) return nullptr; + + int value = preorder[nPreIndex]; + TreeNode* pNode = new TreeNode(value); + nPreIndex++; + int nInIndex = mapIn[value]; + + pNode->left = rebuildTree(preorder, inorder, nL, nInIndex - 1, nPreIndex); + //这里nR边界写错了,不能写preorder.size()。否则右边界会重复遍历 + pNode->right = rebuildTree(preorder, inorder, nInIndex + 1, nR, nPreIndex); + return pNode; +} + +TreeNode* buildTree(vector& preorder, vector& inorder) { + for (int i = 0; i < inorder.size(); i++) { + mapIn[inorder[i]] = i; + } + int nPreIndex = 0; + return rebuildTree(preorder, inorder, 0, inorder.size() - 1, nPreIndex); +} + +//void main() +//{ +// vector preorder = { 3,9,20,15,7 }; +// vector inorder = { 9,3,15,20,7 }; +// buildTree(preorder, inorder); +//} \ No newline at end of file diff --git a/Week_04/ladderLength.cpp b/Week_04/ladderLength.cpp new file mode 100644 index 0000000..dd675bd --- /dev/null +++ b/Week_04/ladderLength.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +using namespace std; +int ladderLength(string beginWord, string endWord, vector& wordList) { + //单向BFS + unordered_set bank, visited;//用set加速查找 + for (int i = 0; i < wordList.size(); i++) { + bank.insert(wordList[i]); + } + if (bank.find(endWord) == bank.end())return 0; + + queue recordBegin; + recordBegin.push(beginWord); + int nSize = 0; + int level = 1;//题目意思包含自身 + string strTmp; + while (!recordBegin.empty()) { + nSize = recordBegin.size(); + while (nSize--) { + strTmp = recordBegin.front(); + recordBegin.pop(); + for (int j = 0; j < strTmp.size(); j++) { + char ch = strTmp[j]; + for (int i = 0; i < 26; i++) {//直接字母替换 + char chNew = i + 'a'; + if (strTmp[j] == chNew) continue; + strTmp[j] = chNew; + if (strTmp == endWord) return ++level; + if (bank.find(strTmp) != bank.end()) { + recordBegin.push(strTmp); + bank.erase(strTmp); + } + } + strTmp[j] = ch; + } + } + ++level; + } + return 0; +} + +//void main() +//{ +// vector bank = { "hot", "dot", "dog", "lot", "log"}; +// ladderLength("hit", "cog", bank); +//} \ No newline at end of file diff --git a/Week_04/numIslands.cpp b/Week_04/numIslands.cpp new file mode 100644 index 0000000..546036c --- /dev/null +++ b/Week_04/numIslands.cpp @@ -0,0 +1,83 @@ +#include + +/*解法: +dfs:时间复杂度O(M*N),空间复杂度O(M*N) +BFS:时间复杂度O(M*N),空间复杂度Omin(M,N)*/ + +using namespace std; + +void dfs(vector>& grid, int row, int col) { + if (grid.size() <= row || row < 0 + || col >= grid[0].size() || col < 0) { + return; + } + if (grid[row][col] != '1') return; + + grid[row][col] = '2';//这样就不用加visited了 + dfs(grid, row, col - 1); + dfs(grid, row, col + 1); + dfs(grid, row - 1, col); + dfs(grid, row + 1, col); +} + +//bfs方法 +//int numIslands(vector>& grid) { +// int numIsland = 0; +// queue> record; +// for (int row = 0; row < grid.size(); row++) { +// for (int col = 0; col < grid[row].size(); col++) { +// if (grid[row][col] == '1') { +// numIsland++; +// record.push(std::make_pair(row, col)); +// while (!record.empty()) { +// std::pair pair = record.front(); +// grid[pair.first][pair.second] = '2'; +// record.pop(); +// if (isIsland(grid, pair.first, pair.second + 1)) { +// record.push(std::make_pair(pair.first, pair.second + 1)); +// grid[pair.first][pair.second + 1] = '2'; +// } +// +// if (isIsland(grid, pair.first, pair.second - 1)) { +// record.push(std::make_pair(pair.first, pair.second - 1)); +// grid[pair.first][pair.second - 1] = '2'; +// } +// +// if (isIsland(grid, pair.first + 1, pair.second)) { +// record.push(std::make_pair(pair.first + 1, pair.second)); +// grid[pair.first + 1][pair.second] = '2'; +// } +// +// if (isIsland(grid, pair.first - 1, pair.second)) { +// record.push(std::make_pair(pair.first - 1, pair.second)); +// grid[pair.first - 1][pair.second] = '2'; +// } +// } +// } +// } +// } +// return numIsland; +//} + +int numIslands(vector>& grid) { + int numIsland = 0; + for (int i = 0; i < grid.size(); i++) { + for (int j = 0; j < grid[i].size(); j++) { + if (grid[i][j] == '1') { + numIsland++; + dfs(grid, i, j); + } + } + } + return numIsland; +} + +void main() +{ + vector> grid; + grid.push_back({ '1', '1', '1', '1', '0' }); + grid.push_back({ '1', '1', '0', '1', '0' }); + grid.push_back({ '1', '1', '0', '0', '0' }); + grid.push_back({ '0', '0', '0', '0', '0' }); + numIslands(grid); +} \ No newline at end of file diff --git a/Week_07/FindWords.cpp b/Week_07/FindWords.cpp new file mode 100644 index 0000000..3f6315a --- /dev/null +++ b/Week_07/FindWords.cpp @@ -0,0 +1,81 @@ +#include +#include + +using namespace std; + +/* +* 实现思路 +* 1、将待查单词放置到字典树中 +* 2、挨个遍历board中的字母 +* 3、每个字母用回溯法判断单词是否有效(注意trie指针,恢复状态) +* 4、判断中,有效的word记录之后置空,防止重复 +* 5、每个字符遍历之后用@字符替换,防止来回查找,结束后恢复 +* 6、老师说的上下左右查找换成数组方式还没试 +* 7、注意:查找到一个单词后不能return返回,还要接着往下找,否则有些情况找不全 +*/ +struct Trie { + string word; + Trie* next[26]; +}; + +vector result; + +size_t nRow = 0; +size_t nCol = 0; + +//回溯法 +void dfs(size_t row, size_t col, Trie* root, vector>& board) { + size_t index = board[row][col] - 'a'; + if (board[row][col] == '@' || root->next[index] == nullptr) return; + root = root->next[index]; + if (root->word != "") { + result.push_back(root->word); + root->word = "";//防止重复单词 + //return;这句话要注释掉,否则对于oath、oathf、oathfk这种,只能查出最短的 + } + + //临时修改访问过的元素,防止重复访问,如果用visited呢? + char ch = board[row][col]; + board[row][col] = '@'; + if (row < nRow - 1) dfs(row + 1, col, root, board); + if (row > 0) dfs(row - 1, col, root, board); + if (col < nCol - 1) dfs(row, col + 1, root, board); + if (col > 0) dfs(row, col - 1, root, board); + board[row][col] = ch; +} +vector findWords(vector>& board, vector& words) { + //把words放入trie中,时间复杂度O(n*m) + + nRow = board.size(); + nCol = nRow ? board[0].size() : 0; + Trie* node = new Trie(); + Trie* root = node; + for (int i = 0; i < words.size(); ++i) { + node = root;//要记得还原node节点 + for (auto ch : words[i]) { + size_t index = ch - 'a'; + if (node->next[index] == nullptr) { + Trie* child = new Trie(); + node->next[index] = child; + } + node = node->next[index]; + } + node->word = words[i]; + } + + //遍历m*n个单词,看看是否能查到 + for (int row = 0; row < board.size(); row++) { + for (int col = 0; col < board[row].size(); col++) { + dfs(row, col, root, board); + } + } + return result; +} + +void main() { + + vector> board = { {'o', 'a', 'a', 'n'} ,{'e', 't', 'a', 'e'},{'i', 'h', 'k', 'r'},{'i', 'f', 'l', 'v'} }; + vector words = { "oath", "pea", "eat", "rain", "hklf", "hf" }; + + findWords(board,words); +} \ No newline at end of file diff --git a/Week_07/Trie.cpp b/Week_07/Trie.cpp new file mode 100644 index 0000000..c8cf9c3 --- /dev/null +++ b/Week_07/Trie.cpp @@ -0,0 +1,61 @@ +#include +#include + +using namespace std; + +class Trie { +private: + bool bIsEnd; + Trie* next[26]; +public: + + /** Initialize your data structure here. */ + Trie() { + bIsEnd = false; + memset(next, 0, sizeof(next)); + } + + /** Inserts a word into the trie. */ + void insert(string word) { + Trie* node = this; + for (int i = 0; i < word.size(); i++) { + size_t ch = word[i] - 'a'; + if (node->next[ch] == nullptr) { + Trie* child = new Trie(); + node->next[ch] = child; + } + node = node->next[ch]; + } + node->bIsEnd = true; + } + + /** Returns if the word is in the trie. */ + bool search(string word) { + Trie* node = this; + for (auto ch : word) { + size_t i = ch - 'a'; + if (node->next[i] == nullptr) return false; + node = node->next[i]; + } + return node->bIsEnd; + } + + /** Returns if there is any word in the trie that starts with the given prefix. */ + bool startsWith(string prefix) { + Trie* node = this; + for (auto ch : prefix) { + size_t i = ch - 'a'; + if (node->next[i] == nullptr) return false; + node = node->next[i]; + } + return true; + } +}; + +/** + * Your Trie object will be instantiated and called as such: + * Trie* obj = new Trie(); + * obj->insert(word); + * bool param_2 = obj->search(word); + * bool param_3 = obj->startsWith(prefix); + */ \ No newline at end of file diff --git a/Week_08/hammingWeight.cpp b/Week_08/hammingWeight.cpp new file mode 100644 index 0000000..ae499cd --- /dev/null +++ b/Week_08/hammingWeight.cpp @@ -0,0 +1,11 @@ +#include + +int hammingWeight(uint32_t n) { + //暴力解法 + int size = 0; + while (n) { + if (n & 1)size++; + n = n >> 1; + } + return size; +} \ No newline at end of file diff --git a/Week_08/isPowerOfTwo.cpp b/Week_08/isPowerOfTwo.cpp new file mode 100644 index 0000000..b47c0df --- /dev/null +++ b/Week_08/isPowerOfTwo.cpp @@ -0,0 +1,5 @@ +bool isPowerOfTwo(int n) { + if (n <= 0) return false; + int r = n & (n - 1); + return r == 0; +} \ No newline at end of file diff --git a/Week_08/reverseBits.cpp b/Week_08/reverseBits.cpp new file mode 100644 index 0000000..bb46f26 --- /dev/null +++ b/Week_08/reverseBits.cpp @@ -0,0 +1,19 @@ +#include + +uint32_t reverseBits(uint32_t n) { + //时间复杂度O(1) + //32位二进制位 power=31 + //依次取出元素,直到n=0,退出n&1,n>>1 + //组合成二进制,result += 2^power,这里应该改成<> 1; + result += bit; + --power; + } + return result; +} \ No newline at end of file