Skip to content

Instantly share code, notes, and snippets.

View SeaHub's full-sized avatar

Seahub SeaHub

View GitHub Profile
@SeaHub
SeaHub / Singleton.m
Created March 3, 2017 01:46
A Good Objc Singleton
//
// Singleton
//
// Created by SeaHub on 2017/3/2.
// Copyright © 2017年 @Seahub All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Singleton : NSObject <NSCopying, NSMutableCopying>
@SeaHub
SeaHub / QuickSort.cpp
Last active March 25, 2017 07:25
QuickSort(C++)
//
// QuickSort
//
// Created by SeaHub on 2017/3/3.
// Copyright © 2017年 @Seahub. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
@SeaHub
SeaHub / Alloc.m
Last active March 4, 2017 07:00
Alloc realization in GNUStep Foundation
#define AADD(c, o) GSDebugAllocationAdd(c, o)
static SEL cxx_construct, cxx_destruct;
/**
* Calls the C++ constructors for this object, starting with the ones declared
* in aClass. The compiler generates two methods on Objective-C++ classes that
* static instances of C++ classes as ivars. These are -.cxx_construct and
* -.cxx_destruct. The -.cxx_construct methods must be called in order from
* the root class to all subclasses, to ensure that subclass ivars are
* initialised after superclass ones. This must be done in reverse for
@SeaHub
SeaHub / Retain.m
Last active March 4, 2017 06:59
Retain realization in GNUStep Foundation
/**
* Increments the reference count and returns the receiver.<br />
* The default implementation does this by calling NSIncrementExtraRefCount()
*/
- (id)retain {
#if (GS_WITH_GC == 0)
NSIncrementExtraRefCount(self);
#endif
return self;
}
@SeaHub
SeaHub / Release.m
Last active March 4, 2017 06:59
Release realization in GNUStep Foundation
/**
* Decrements the retain count for the receiver if greater than zero,
* otherwise calls the dealloc method instead.<br />
* The default implementation calls the NSDecrementExtraRefCountWasZero()
* function to test the extra reference count for the receiver (and
* decrement it if non-zero) - if the extra reference count is zero then
* the retain count is one, and the dealloc method is called.<br />
* In GNUstep, the [NSObject+enableDoubleReleaseCheck:] method may be used
* to turn on checking for ratain/release errors in this method.
*/
@SeaHub
SeaHub / Dealloc.m
Last active March 4, 2017 07:04
Dealloc realization in GNUStep Foundation
/**
* Deallocates the receiver by calling NSDeallocateObject() with self
* as the argument.<br />
* <p>
* You should normally call the superclass implementation of this method
* when you override it in a subclass, or the memory occupied by your
* object will not be released.
* </p>
* <p>
* <code>NSObject</code>'s implementation of this method
@SeaHub
SeaHub / BinarySearch.cpp
Last active March 4, 2017 09:36
BinarySearch(C++)
//
// BinarySearch
//
// Created by SeaHub on 2017/3/4.
// Copyright © 2017年 @Seahub. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
@SeaHub
SeaHub / HeapSort.cpp
Last active March 11, 2017 12:57
HeapSort(C++)
//
// HeapSort
//
// Created by SeaHub on 2017/3/11.
// Copyright © 2017年 @Seahub. All rights reserved.
//
#include <vector>
#include <iostream>
@SeaHub
SeaHub / TraverseTreeOnLevel(C++).cpp
Created March 12, 2017 03:01
nowcoder exercise
// 有一棵二叉树,请设计一个算法,按照层次打印这棵二叉树。
// 给定二叉树的根结点root,请返回打印结果,结果按照每一层一个数组进行储存,
// 所有数组的顺序按照层数从上往下,且每一层的数组内元素按照从左往右排列。保证结点数小于等于500。
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
@SeaHub
SeaHub / JudgeRotatedString(C++).cpp
Last active March 13, 2017 07:32
nowcoder exercise
// 如果对于一个字符串A,将A的前面任意一部分挪到后边去形成的字符串称为A的旋转词。
// 比如A="12345",A的旋转词有"12345","23451","34512","45123"和"51234"。
// 对于两个字符串A和B,请判断A和B是否互为旋转词。
// 给定两个字符串A和B及他们的长度lena,lenb,请返回一个bool值,代表他们是否互为旋转词。
class Rotation {
public:
bool chkRotation(string A, int lena, string B, int lenb) {
if (lena != lenb) {
return false;