Skip to content

Instantly share code, notes, and snippets.

View icameling's full-sized avatar
🎯
Focusing

icameling

🎯
Focusing
View GitHub Profile
@icameling
icameling / cppbinary.cc
Created April 28, 2019 12:48 — forked from molpopgen/cppbinary.cc
Basics of binary I/O in C++
/*
Example of how to write binary stuff in C++
Illustrates some of the various quirks/annoyances
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
@icameling
icameling / binary_search_704.cpp
Last active July 18, 2022 08:05
#数组 #二分查找 #leetcode
class Solution {
public:
int search(vector<int>& nums, int target) {
if (nums[0] > target || nums.back() < target)
return -1;
int left = 0, right = nums.size() - 1;
// [left, right]
while (left <= right) {
int mid = left + (right - left) / 2;
@icameling
icameling / remove_element_27.cpp
Last active July 18, 2022 08:06
#数组 #移除元素 #leetcode
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
auto iter = nums.begin();
for (;iter != nums.end();) {
if (*iter == val) {
iter = nums.erase(iter);
} else {
iter++;
}
@icameling
icameling / squares-of-a-sorted-array_977.cpp
Last active July 18, 2022 08:06
#数组 #有序数据的平方 #leetcode
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int i = 0, j = nums.size() - 1;
int k = 0;
vector<int> rets(nums.size(), 0);
for (int k = nums.size() - 1; k >=0; k--) {
int ii = nums[i] * nums[i];
int jj = nums[j] * nums[j];
@icameling
icameling / minimum-size-subarray-sum-209.cpp
Last active July 18, 2022 08:06
#数组 #长度最小的子数组 #leetcode
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int sub_len = nums.size() + 1;
int i = 0;
int sum = 0;
for (int j = 0; j < nums.size(); ++j) {
sum += nums[j];
while (sum >= target){
int len = j - i + 1;
@icameling
icameling / spiral-matrix-ii-59.cpp
Last active July 18, 2022 08:06
#数组 #螺旋矩阵 #leetcode
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
// 行 列
vector<vector<int>> mat;
for (int i = 0; i < n; ++i) {
vector<int> tmp;
for (int j = 0; j < n; ++j)
tmp.push_back(0);
mat.push_back(tmp);
@icameling
icameling / remove-linked-list-elements-203.cpp
Last active July 18, 2022 08:04
#链表 #移除链表元素 #leetcode
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
@icameling
icameling / design-linked-list.cpp
Last active July 18, 2022 08:04
#链表 #设计链表
class MyLinkedList {
struct Node {
int val;
struct Node* next;
Node(): val(0), next(NULL) {}
Node(int v): val(v), next(NULL) {}
};
Node* head_;
int size_;
@icameling
icameling / reverse-linked-list.cpp
Last active July 18, 2022 08:04
#链表 #反转链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
@icameling
icameling / swap-nodes-in-pairs.cpp
Created July 18, 2022 08:47
#链表 #两两交换链表中的节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/