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–