Skip to content

Instantly share code, notes, and snippets.

View qiaoxu123's full-sized avatar
:shipit:
Hard working

xqiao qiaoxu123

:shipit:
Hard working
  • China
View GitHub Profile
@qiaoxu123
qiaoxu123 / c_const.c
Created July 31, 2019 05:43
[参考链接](https://qiaoxu123.github.io/post/3-C++Courses/) `const`变量在C/C++中有较大的区别。对于C语言来讲,`const`定义仅仅是一个只读变量,可以通过指针进行修改。而C++中对`const`参数进行了升级处理,使用类似宏定义的方式进行了常量化。为了向上兼容,也保留了原有的一些性质。具体看参考链接。
#include<stdio.h>
int main(){
const int c = 0;
int* p = (int*)&c;
printf("Begin...\n");
*p = 5;
printf("c = %d\n",c);
printf("End...\n");
@qiaoxu123
qiaoxu123 / test.cpp
Created July 30, 2019 07:19
C++ 中explicit 参数的作用是为了防止C++函数在执行时进行隐式转换。下方代码会报错,注释部分不会报错。 [参考地址](https://blog.csdn.net/tiefanhe/article/details/11478901)
#include<iostream>
// class Base {
// public:
// Base() = default;
// Base(int num){};
// private:
// int num;
// };
@qiaoxu123
qiaoxu123 / default.cpp
Last active July 29, 2019 05:38
`default`函数存在的问题,在于用户想要自定义函数进行类的初始化。此时类的默认构造函数需要显示进行定义。这是用户自定义的无参数的默认构造函数 此时定义的默认构造函数的功能和类模板原始的自定义构造函数的功能是相同的,但是效率变得非常低。在这使用`default`关键字,将用户自定义的无参数 的构造函数进行处理,这样编译器在处理的时候可以极大地提升效率。 --- [参考链接](https://www.ibm.com/developerworks/cn/aix/library/1212_lu
#include <iostream>
using namespace std;
class base {
public:
base() = default;
~base() = default;
private:
int num;
};
@qiaoxu123
qiaoxu123 / myCode.cpp
Created March 22, 2019 08:16
> 简单粗暴的for循环
//Runtime: 12 ms, faster than 57.21%
//Memory Usage: 10.5 MB, less than 48.01%
class Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string> array;
for(int i = 1;i <= n;++i){
if(i % 3 == 0 && i % 5 != 0)
array.push_back("Fizz");
@qiaoxu123
qiaoxu123 / log.cpp
Created March 20, 2019 23:47
> 求3的n次方。题目实现的最简单方式是使用循环或者递归来实现,从而判断这个数值是否是3的n次方。 但题目本身提出,能够不使用循环或者递归,在这使用数学log来实现
//Runtime: 52 ms, faster than 96.57%
//Memory Usage: 8.5 MB, less than 5.67%
class Solution {
public:
bool isPowerOfThree(int n) {
return fmod(log10(n) / log10(3),1) == 0;
}
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@qiaoxu123
qiaoxu123 / myCode.cpp
Last active March 16, 2019 11:59
> 经典的动态规划题目 第一种方法是采用从头进行相加的方法进行,当总和小于等于0时,将之前的数记为最大值。重复判断即可。 缺点:复杂度很高 第二种方法:使用一个循环实现,但效率上还是没有提高。。
//Runtime: 16 ms, faster than 20.14%
//Memory Usage: 10.3 MB, less than 60.30%
class Solution {
public:
int maxSubArray(vector<int>& nums) {
if(nums.empty()) return NULL;
if(nums.size() == 1) return nums[0];
int sum = 0;
@qiaoxu123
qiaoxu123 / BinarySearch.cpp
Last active March 13, 2019 00:48
> 排序查找类问题。 最简单的方法是使用暴力查找,从0开始找,找到第一个值为true的数即可。但是这样效率太低。于是尝试用查找方法 二分查找是最常用的查找方式,在这需要注意的是判断初始值是否为true,此时第一个产品为假,则后续判断无效。
//Runtime: 4 ms, faster than 100.00%
//Memory Usage: 8.2 MB, less than 17.32%
class Solution {
public:
int firstBadVersion(int n) {
long begin = 1;
long end = n;
long temp;
if(isBadVersion(1)) return 1;
@qiaoxu123
qiaoxu123 / sort.cpp
Created March 11, 2019 13:29
> 非常经典的排序题目,两个数组进行插入排序
//Runtime: 8 ms, faster than 92.35%
//Memory Usage: 9.1 MB, less than 52.67%
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
if(!nums2.empty()){
for(int i = m,j = 0;i < m+n;i++,j++){
nums1[i] = nums2[j];
}