0%

#include <stdio.h>
#define maxsize 10
#define failure -1
#define success 1
typedef int datatype;
typedef struct {
    datatype data[maxsize];
    int length;
} Seqlist;
typedef Seqlist* List;
///求表长度 o(1)
int length(Seqlist L) {
    return L.length;
}
/// 取表元 O(n)
datatype get(List L, int index) {
    if (index < 1 || index > L->length ) {
        return failure;
    }
    return L->data[index - 1];
}
/// 定位 O(n)
int locate(List L, datatype x) {
// for (int i = 0; i < L->length - 1; i++) {
// if (x == L->data[i]) {
// return i + 1;
// }
// }
// return failure;
    int i = 0;
    while (i < L->length && L->data[i] != x) {
        i++;
    }
    if (i < L->length) {
        return i + 1;
    } else {
        return failure;
    }
}
/// 删除 O(n)
int delete(List L, int index) {
    if (index < 1 || index > L->length) {
        return failure;
    }
    for (int j = index; j < L->length; j++) {
        L->data[j - 1] = L->data[j];
    }
    return success;
}
/// 插入 O(n)
int insert(List L, datatype x, int index) {
    if (L->length == maxsize) {
        return failure; //表满
    }
    if (index < 1 || index > L->length + 1) {
        return failure; //位置错
    }
    for (int j = L->length; j >= index; j--) {
        L->data[j] = L->data[j - 1];
    }
    L->data[index - 1] = x;
    L->length++;
    return success;
}
int main(int argc, const char * argv[]) {
    List L = (List)malloc(sizeof(Seqlist));
    L->length = 0;
    printf("%d\n", length(*L));
    int result = insert(L, 0, 1);
    printf("%d\n", result);
    printf("%d\n", length(*L));
    printf("取表元 %d\n", get(L, 0));
    return 0;
}

–EOF–

import Foundation
//dynamicMemberLookup_keyPath
struct Address {
    let street: String
    let zipCode: String
    let city: String
}
@dynamicMemberLookup
struct Person {
    let firstName: String
    let lastName: String
    private let address: Address
    init(firstName: String, lastName: String, address: Address) {
        self.firstName = firstName
        self.lastName = lastName
        self.address = address
    }
    subscript<T>(dynamicMember keyPath: KeyPath<Address, T>) -> T {
        self.address[keyPath: keyPath]
    }
}
let address = Address(street: "青岛街", zipCode: "266700", city: "北京市")
let person = Person(firstName: "五", lastName: "王", address: address)
print(person.firstName)
//property_wrappers
//https://www.avanderlee.com/swift/property-wrappers/
extension UserDefaults {
    public enum Keys {
        static let hasSeenAppIntroduction = "has_seen_app_introduction"
    }
    var hasSeenAppIntroduction: Bool {
        set {
            set(newValue, forKey: Keys.hasSeenAppIntroduction)
        }
        get {
            bool(forKey: Keys.hasSeenAppIntroduction)
        }
    }
}
UserDefaults.standard.hasSeenAppIntroduction = true
print(UserDefaults.standard.hasSeenAppIntroduction)
@propertyWrapper
struct UserDefault<T> {
    let key: String
    let defaultValue: T
    init(_ key: String, defaultValue: T) {
        self.key = key
        self.defaultValue = defaultValue
    }
    var value: T {
        get {
            UserDefaults.standard.value(forKey: key) as? T ?? defaultValue
        }
        set {
            UserDefaults.standard.set(newValue, forKey: key)
        }
    }
}
struct UserDefaultsConfig {
    @UserDefault("has_seen_app_introduction", defaultValue: false)
    static var hasSeenAppIntroduction: Bool
}
print(UserDefaultsConfig.hasSeenAppIntroduction)

–EOF–

var nums = [1, -2, 2, -13, 6, -10, 3, 12]
/// O(n^3)
func maxSubSequenceSum1() {
    var maxSum = 0
    for i in 0..<nums.count {
        for j in i..<nums.count {
            var thisSum = 0
            for k in i...j {
                thisSum += nums[k]
            }
            if thisSum > maxSum {
                maxSum = thisSum
            }
        }
    }
    print(maxSum)
}
/// O(n^2)
func maxSubSequenceSum2() {
    var maxSum = 0
    for i in 0..<nums.count {
        var thisSum = 0
        for j in i..<nums.count {
            thisSum += nums[j]
            if thisSum > maxSum {
                maxSum = thisSum
            }
        }
    }
    print(maxSum)
}
/// O(n)
func maxSubSequenceSum3() {
    var maxSum = 0, thisSum = 0
    for i in 0..<nums.count {
        thisSum += nums[i]
        if thisSum > 0 {
            maxSum = thisSum
        } else { //如果thisSum小于0,则不可能使后面的部分和增大,抛弃.
            thisSum = 0
        }
    }
    print(maxSum)
}
maxSubSequenceSum1()
maxSubSequenceSum2()
maxSubSequenceSum3()

算法很重要呐 😄😄

–EOF–

总感觉用账号密码提交代码是反人类的🙂

解决方法:

SourceTree -> 偏好设置-> Git -> 使用系统安装的Git

好长时间不写博客了,能水一篇是一篇.

–EOF–

位运算了解一下,哈哈哈哈哈哈…

Person.swift

import Foundation
extension Bool {
    init(_ integer: Int){
        self.init(integer != 0)
    }
}
struct Person {
    let TallMask = (0b00000001 << 0b00000000)
    let RichMask = (0b00000001 << 0b00000001)
    let HandsomeMask = (0b00000001 << 0b00000010)
    var _tallRichHandsome = 0b00000000
    mutating func setTall(_ tall: Bool) {
        if tall {
            _tallRichHandsome = (_tallRichHandsome | TallMask)
        } else {
            _tallRichHandsome = (_tallRichHandsome & ~TallMask)
        }
    }
    func isTall() -> Bool {
        return Bool(_tallRichHandsome & TallMask)
    }
    mutating func setRich(_ rich: Bool) {
        if rich {
            _tallRichHandsome = (_tallRichHandsome | RichMask)
        } else {
            _tallRichHandsome = (_tallRichHandsome & ~RichMask)
        }
    }
    func isRich() -> Bool {
        return Bool(_tallRichHandsome & RichMask)
    }
    mutating func setHandsome(_ handsome: Bool) {
        if handsome {
            _tallRichHandsome = (_tallRichHandsome | HandsomeMask)
        } else {
            _tallRichHandsome = (_tallRichHandsome & ~HandsomeMask)
        }
    }
    func isHandsome() -> Bool {
        return Bool(_tallRichHandsome & HandsomeMask)
    }
}

main.swift

import Foundation
var person = Person()
print(person.isTall())
print(person.isRich())
print(person.isHandsome())
print("================")
print(person._tallRichHandsome)
print("===================")
person.setTall(true)
person.setRich(true)
person.setHandsome(true)
print(person.isTall())
print(person.isRich())
print(person.isHandsome())
print("===================")
//print(person.TallMask)
//print(person.RichMask)
//print(person.HandsomeMask)
person.setTall(false)
print(person.isTall())
print(person.isRich())
print(person.isHandsome())
print("==================")
//print(0b00000000)
//print(0b00000001)
//print(0b00000010)
print("==================")
print(person._tallRichHandsome)

–EOF–

//实例对象(instance对象)
    //isa指针
    //变量
//类对象(Class对象)
    //每个类有且只有一个类对象
    //class对象在内存中存储的主要信息有:
    //isa指针
    //superclass指针
    //类属性信息(@property), 类的对象信息(instance method)
    //类的协议信息(protocol),成员变量信息(ivar)
//元类对象(meta class)
    //每个类只有一个元类对象
    //元类对象跟类对象内存结构是一样的
    //包括:
    //isa指针
    //superclass指针
    //类的类方法信息 (class method,+号开头)
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //实例对象(instance对象)
        NSObject *obj1 = [[NSObject alloc] init];
        NSObject *obj2 = [[NSObject alloc] init];
        //类对象(Class对象)
        Class objClass1 = [obj1 class]; //类对象
        Class objClass2 = [obj2 class];
        Class objClass3 = object_getClass(obj2);
        Class objClass4 = object_getClass(obj1);
        Class objClass5 = [NSObject class];
        NSLog(@"%p", objClass1);
        NSLog(@"%p", objClass2);
        NSLog(@"%p", objClass3);
        NSLog(@"%p", objClass4);
        NSLog(@"%p", objClass5);
        //元类对象(meta class对象)
        Class metaClass = object_getClass([NSObject class]);
    }
    return 0;
}

–EOF–

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.

import Foundation
//协议语法
protocol SomeProtocol {
}
struct SomeStructure: SomeProtocol {}
class SomeClass: SomeProtocol {}
//属性要求
//如果属性标注了(set get),则不能通过常量存储属性和只读计算属性来实现
//如果属性只设置了(get),那么任何类型都可以满足,包括变量存储属性
protocol SomeProtocol1 {
    var mustBeSettable: Int { get set }
    var doesNotNeedToBeSettable: Int { get }
}
//协议中用static修饰时,实现中用static或者class实现都行
protocol AnotherProtocol {
    static var someTypeProperty: Int { get set }
}
protocol FullyNamed {
    var fullName: String { get }
}
struct Person: FullyNamed {
    var fullName: String
}
//可set 可 get
let john = Person(fullName: "Tom")
class Starship: FullyNamed {
    var prefix: String?
    var name: String
    init(name: String, prefix: String? = nil) {
        self.name = name
        self.prefix = prefix
    }
    var fullName: String {
        return (prefix != nil ? prefix!+" " : "") + name
    }
}
var ncc1701 = Starship(name: "Enterprise", prefix: "USS")
//方法要求
//不能在协议方法中指定默认值
protocol SomeProtocol2 {
    static func someTypeMethod()
}
protocol RandomNumberGenerator {
    func random() -> Double
}
//线性同余生成器的伪随机数生成器算法,好高大上有木有...
class LinearCongruentialGenerator: RandomNumberGenerator {
    var lastRandom = 42.0
    let m = 139968.0
    let a = 3877.0
    let c = 29573.0
    func random() -> Double {
        lastRandom = ((lastRandom * a + c).truncatingRemainder(dividingBy: m))
        return lastRandom / m
    }
}
let generator = LinearCongruentialGenerator()
print(generator.random())
print(generator.random())
//变异方法要求 mutating method requirements
//如果希望实现协议的任何类型(类,结构体,枚举)要修改实例或者实例中的任何属性
//得在func前加mutating关键字
//类实现时,无需加mutating,结构体,枚举需要加
protocol Toggleable {
    mutating func toggle()
}
enum OnOffSwitch: Toggleable {
    case off, on
    mutating func toggle() {
        switch self {
        case .on:
            self = .off
        case .off:
            self = .on
        }
    }
}
var lightSwitch = OnOffSwitch.off
print(lightSwitch)
lightSwitch.toggle()
print(lightSwitch)
//初始化程序要求
protocol SomeProtocol3 {
    init(someParameter: Int)
}
//required 方便子类继承,从而遵守SomeProtocol3协议
//如果给类加上final,则required可以省略
final class SomeClass3: SomeProtocol3 {
    required init(someParameter: Int) {
        //在这进行初始化实现
    }
}
//如果子类重写父类中init方法,并且这个init也是协议中的实现,则需要用
//`required`和`override`修饰
protocol SomeProtocol4 {
    init()
}
class SomeSuperClass {
    init() {
    }
}
class SomeSubClass: SomeSuperClass, SomeProtocol4 {
    required override init() {
        //good job
    }
}
//协议还可以定义可失败构造器
//协议作为类型
//协议本身不实现任何功能,但是,创建的任何协议都会变成代码中成熟的类型.
//可以用协议的地方:
//1. 在函数,方法或初始化方法中作为参数或返回值
//2. 作为常量,变量,或者属性的类型
//3. 作为数组,字典或其他容器类型的元素类型
class Dice {
    let sides: Int
    let generator: RandomNumberGenerator
    init(sides: Int, generator: RandomNumberGenerator) {
        self.sides = sides
        self.generator = generator
    }
    func roll() -> Int {
        return Int(generator.random() * Double(sides)) + 1
    }
}
var d6 = Dice(sides: 6, generator: LinearCongruentialGenerator())
for _ in 1...5 {
    print("Random dice roll is \(d6.roll())")
}
//委托
//响应特定操作或者从外部检索数据
protocol DiceGame {
    var dice: Dice { get }
    func play()
}
protocol DiceGameDelegate: AnyObject {
    func gameDidStart(_ game: DiceGame)
    func game(_ game: DiceGame, didStartNewTurnWithDiceRoll diceRoll: Int)
    func gameDidEnd(_ game: DiceGame)
}
class SnakesAndLadders: DiceGame {
    let finalSquare = 25
    let dice = Dice(sides: 6, generator: LinearCongruentialGenerator())
    var square = 0
    var board: [Int]
    init() {
        board = Array(repeating: 0, count: finalSquare + 1)
        board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
        board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
    }
    weak var delegate: DiceGameDelegate?
    func play() {
        square = 0
        delegate?.gameDidStart(self)
        gameLoop: while square != finalSquare {
            let diceRoll = dice.roll()
            delegate?.game(self, didStartNewTurnWithDiceRoll: diceRoll)
            switch square + diceRoll {
            case finalSquare:
                break gameLoop
            case let newSquare where newSquare > finalSquare:
                continue gameLoop
            default:
                square += diceRoll
                square += board[square]
            }
        }
        delegate?.gameDidEnd(self)
    }
}
class DiceGameTracker: DiceGameDelegate {
    var numberOfTurns = 0
    func gameDidStart(_ game: DiceGame) {
        numberOfTurns = 0
        if game is SnakesAndLadders {
            print("Started a new game of Snakes and Ladders")
        }
        print("The game is using a \(game.dice.sides)-sided dice")
    }
    func game(_ game: DiceGame, didStartNewTurnWithDiceRoll diceRoll: Int) {
        numberOfTurns += 1
        print("Rolled a \(diceRoll)")
    }
    func gameDidEnd(_ game: DiceGame) {
        print("The game lasted for \(numberOfTurns) turns")
    }
}
let tracker = DiceGameTracker()
let game = SnakesAndLadders()
game.delegate = tracker
game.play()
//通过扩展遵守某个协议
protocol TextRepresentable {
    var textualDescription: String { get }
}
extension Dice: TextRepresentable {
    var textualDescription: String {
        return "A \(sides)-sided dice"
    }
}
let d12 = Dice(sides: 12, generator: LinearCongruentialGenerator())
print(d12.textualDescription)
// Prints "A 12-sided dice"
extension SnakesAndLadders: TextRepresentable {
    var textualDescription: String {
        return "A game of Snakes and Ladders with \(finalSquare) squares"
    }
}
print(game.textualDescription)
//有条件的遵守协议
extension Array: TextRepresentable where Element: TextRepresentable {
    var textualDescription: String {
        let itemsAsText = self.map { $0.textualDescription }
        return "[" + itemsAsText.joined(separator: ", ") + "]"
    }
}
let myDice = [d6, d12]
print(myDice.textualDescription)
//通过协议适应某种类型
//如果某个类型已经符合协议的所有要求,但尚未声明它采用该协议,则可以使其采用带有空扩展的协议
struct Hamster {
    var name: String
    var textualDescription: String {
        return "A hamster named \(name)"
    }
}
extension Hamster: TextRepresentable {}
let simonTheHamster = Hamster(name: "Simon")
let somethingTextRepresentable: TextRepresentable = simonTheHamster
print(somethingTextRepresentable.textualDescription)
// Prints "A hamster named Simon"
//协议类型的集合
let things: [TextRepresentable] = [game, d12, simonTheHamster]
for thing in things {
    print(thing.textualDescription)
}
//协议继承

还是有点烧脑….

–EOF–