c_cpp 打包/打开LE / BE

打包/解包Little和Big Endian无符号整数uint16_t,uint32_t,uint64_t

unpack.h
/********************************************************************************
*
*  Copyright (c) Simon Downey <simon@ark.io> <https://github.com/sleepdefic1t>
*
*  The MIT License (MIT)
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy of
*  this software and associated documentation files (the "Software"), to deal in
*  the Software without restriction, including without limitation the rights to
*  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
*  the Software, and to permit persons to whom the Software is furnished to do so,
*  subject to the following conditions:
*
*  The above copyright notice and this permission notice shall be included in all
*  copies or substantial portions of the Software.
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
*  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
*  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
*  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
*  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*********************************************************************************/
 

#include <stdint.h>

//////////////////////////////////////////////////////////////////////

#define FLAG_64_BYTE        0xFFFFFFFFFFFFFFFF
#define FLAG_32_BYTE        0xFFFFFFFF
#define FLAG_16_BYTE        0xFFFF
#define FLAG_8_BYTE         0xFF

//////////////////////////////////////////////////////////////////////

uint16_t unpack2LE(uint8_t *src) {
  return ((uint16_t)src[0] & FLAG_8_BYTE)      |
         ((uint16_t)src[1] & FLAG_8_BYTE) << 8U;
}

uint32_t unpack4LE(uint8_t *src) {
  return ((uint32_t)src[0] & FLAG_8_BYTE)          |
         ((uint32_t)src[1] & FLAG_8_BYTE)   <<  8U |
         ((uint32_t)src[2] & FLAG_16_BYTE)  << 16U |
         ((uint32_t)src[3] & FLAG_32_BYTE)  << 24U;
}

uint64_t unpack8LE(uint8_t *src) {
  return ((uint64_t)src[0] & FLAG_8_BYTE)          |
         ((uint64_t)src[1] & FLAG_8_BYTE)   <<  8U |
         ((uint64_t)src[2] & FLAG_16_BYTE)  << 16U |
         ((uint64_t)src[3] & FLAG_32_BYTE)  << 24U |
         ((uint64_t)src[4] & FLAG_32_BYTE)  << 32U |
         ((uint64_t)src[5] & FLAG_64_BYTE)  << 40U |
         ((uint64_t)src[6] & FLAG_64_BYTE)  << 48U |
         ((uint64_t)src[7] & FLAG_64_BYTE)  << 56U;
}

//////////////////////////////////////////////////////////////////////

uint16_t unpack2BE(uint8_t *src) {
  return ((uint16_t)src[0] & FLAG_8_BYTE)   <<  8U |
         ((uint16_t)src[1] & FLAG_8_BYTE);
}

uint32_t unpack4BE(uint8_t *src) {
  return ((uint32_t)src[0] & FLAG_32_BYTE)  << 24U |
         ((uint32_t)src[1] & FLAG_16_BYTE)  << 16U |
         ((uint32_t)src[2] & FLAG_8_BYTE)   <<  8U |
         ((uint32_t)src[3] & FLAG_8_BYTE);
}

uint64_t unpack8BE(uint8_t *src) {
  return ((uint64_t)src[0] & FLAG_64_BYTE)  << 56U |
         ((uint64_t)src[1] & FLAG_64_BYTE)  << 48U |
         ((uint64_t)src[2] & FLAG_64_BYTE)  << 40U |
         ((uint64_t)src[3] & FLAG_32_BYTE)  << 32U |
         ((uint64_t)src[4] & FLAG_32_BYTE)  << 24U |
         ((uint64_t)src[5] & FLAG_16_BYTE)  << 16U |
         ((uint64_t)src[6] & FLAG_8_BYTE)   <<  8U |
         ((uint64_t)src[7] & FLAG_8_BYTE);
}

//////////////////////////////////////////////////////////////////////

c_cpp 54.螺旋矩阵

0054.cpp
class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector<int> res;
        if (matrix.empty() || matrix.size() == 0 || matrix[0].empty() || matrix[0].size() == 0) return res;
        int rowBegin = 0;
        int rowEnd = matrix.size() - 1;
        int colBegin = 0;
        int colEnd = matrix[0].size() - 1;
        
        // loop over 2 ~ 4 steps
        while (rowBegin <= rowEnd && colBegin <= colEnd) {
            for (int i = colBegin; i <= colEnd; i++) {
                res.push_back(matrix[rowBegin][i]);
            }
            rowBegin++;   // all elements in first row ended
            
            for (int i = rowBegin; i <= rowEnd; i++) {
                res.push_back(matrix[i][colEnd]);
            }
            colEnd--;   // all elements in last row ended
            
            if (rowBegin <= rowEnd) {
                for (int i = colEnd; i >= colBegin; i--) {
                    res.push_back(matrix[rowEnd][i]);
                }
            }
            rowEnd--;
            
            if (colBegin <= colEnd) {
                for (int i = rowEnd; i >= rowBegin; i--) {
                    res.push_back(matrix[i][colBegin]);
                }
            }
            colBegin++;
        }
        
        return res;
    }
};

c_cpp 字符串反转

strrev.cpp
#include <iostream>
#include <cstring>
using namespace std;

int main() {
	char s[] = "hello";
	_strrev(s);
	cout << s << endl;	// olleh
	return 0;
}
reverse.cpp
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
	string s = "hello";
	reverse(s.begin(), s.end());
	cout << s << endl;	// olleh
	return 0;
}

c_cpp 163.失踪范围

0163.cpp
class Solution {
public:
    vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
        vector<string> res;
        long alower = (long)lower;
        long aupper = (long)upper;
        for (int num : nums) {
            if (num == alower) {
                alower++;
            }
            else if (alower < num) {
                if (alower + 1 == num){
                    res.push_back(to_string(alower));
                }
                else {
                    res.push_back(to_string(alower) + "->" + to_string(num - 1));
                }
                alower = (long)num + 1;
            }
        }
        if (alower == aupper) res.push_back(to_string(alower));
        else if (alower < aupper) res.push_back(to_string(alower) + "->" + to_string(aupper));
        return res;
    }
};

c_cpp 集合的交并差运算

- [set_union的几个例子](https://www.cnblogs.com/s1124yy/p/5849553.html) <br/>

set.cpp
#include <iostream>
#include <algorithm>	// set_union
#include <iterator>		// inserter
#include <set>
using namespace std;

int main() {
	set<int> s1 = { 1, 2, 3, 4, 5 };
	set<int> s2 = { 3, 4, 5, 6, 7 };
	set<int> res;

	set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(res, res.begin()));
	for (auto val : res) {
		cout << val << ' ';
	}
	cout << endl;
	// 3 4 5 

	set<int>().swap(res);
	set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(res, res.begin()));
	for (auto val : res) {
		cout << val << ' ';
	}
	cout << endl;
	// 1 2 3 4 5 6 7 

	set<int>().swap(res);
	set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(res, res.begin()));
	for (auto val : res) {
		cout << val << ' ';
	}
	cout << endl;
	// 1 2 
}

c_cpp 素数判定

prime.cpp
bool isPrime(int x) {
	if (x <= 1) {
		return false;
	}
	for (int i = 2; i * i <= x; i++) {
		if (x % i == 0) {
			return false;
		}
	}
	return true;
}

c_cpp 分数加法

fraction.cpp
#include <iostream>
using namespace std;
typedef long long ll;

int N;
const int MAX_N = 100 + 5;
struct Fraction {
	ll up, down;
};

ll gcd(ll a, ll b) {
	if (b == 0) {
		return a;
	}
	else {
		return gcd(b, a % b);
	}
}

void reduction(Fraction &r) {
	if (r.down < 0) {
		r.up = -r.up;
		r.down = -r.down;
	}
	if (r.up == 0) {
		r.down = 1;
	}
	else {
		ll d = gcd(abs(r.up), abs(r.down));
		r.up /= d;
		r.down /= d;
	}
}

Fraction add(Fraction f1, Fraction f2) {
	Fraction res;
	res.up = f1.up * f2.down + f1.down * f2.up;
	res.down = f1.down * f2.down;
	reduction(res);
	return res;
}

void print(Fraction f) {
	reduction(f);
	if (f.down == 1) {
		cout << f.up << endl;
	}
	else if (abs(f.up)>f.down) {
		cout << f.up / f.down << " " << abs(f.up) % f.down << "/" << f.down << endl;
	}
	else {
		cout << f.up << "/" << f.down << endl;
	}
}

int main() {
	Fraction sum, f;
	sum.up = 0;
	sum.down = 1;
	cin >> N;
	for (int n = 0; n < N; n++) {
		cin >> f.up;
		getchar();
		cin >> f.down;
		reduction(f);
		// cout << f.up << "/" << f.down << endl;
		sum = add(sum, f);
	}
	print(sum);
}

/*
Sample Input 1:
5
2/5 4/15 1/30 -2/60 8/3
Sample Output 1:
3 1/3
Sample Input 2:
2
4/3 2/3
Sample Output 2:
2
Sample Input 3:
3
1/3 -1/6 1/8
Sample Output 3:
7/24
*/

c_cpp gcd和lcm

gcd.cpp
int gcd(int a, int b) {
	if (b == 0) {
		return a;
	}
	else {
		return gcd(b, a % b);
	}
}

c_cpp 检查字符串是否为空

.c
 char frame_label_text[40] = "";
    strcat(frame_label_text, "hi");
    if (frame_label_text[0] == '\0')
      {
        printf("no text");
      }
    else{
        printf("there is text");
    }
      

c_cpp 二进制搜索模板

binary_search.cpp
int solve(int left, int right) {
	int mid;
	while (left < right) {
		mid = left + (right - left) / 2;;
		if (/*条件成立*/) {
			/*
			 * lower_bound(): a[mid] >= val
			 * upper_bound(): a[mid] > val
			 */
			right = mid;
		}
		else {
			left = mid + 1;
		}
	}
	return left;
}