2017年6月25日 星期日

[LeetCode] Two sum (easy)

twosum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.

給定兩個非空的鍊結串列,分別代表兩個非負的整數串列,串列內每個元素皆由反方向儲存(越前面的節點位數越低),每一個節點代表一個位元。
將此兩個鍊結串列相加並回傳。

Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

參考答案:

int* twoSum(int* nums, int numsSize, int target){
    int *ans = malloc(sizeof(int)*2);
    for(int i=0;i<numsSize;i++){
        for(int j=0;j<numsSize;j++){
            if(target == (nums[i] + nums[j]) && i!=j ){
                ans[0] = i,ans[1] = j;
                return ans;
            }
        }
    }
    return ans;
}

學習目的:

學習兩個以上的回傳值型態宣告方法 malloc
c++ define:
void* malloc (size_t size);
//example
int *data = malloc(1000, sizeof(int)); //宣告
free(data); //釋放
測試程式:
//測試程式
int* twoSum(int* nums, int numsSize, int target){
    int *ans = malloc(sizeof(int)*2);
    for(int i=0;i<numsSize;i++){
        for(int j=0;j<numsSize;j++){
            if(target == (nums[i] + nums[j]) && i!=j ){
                ans[0] = i,ans[1] = j;
                return ans;
            }
        }
    }
    return ans;
}


int main(){
    int nums[] = {2, 7, 11, 15};
    int target = 9;

    printf("sizeof(nums) = %1lu \n", sizeof(nums)/sizeof(int));

    int *out = twoSum(nums, sizeof(nums)/sizeof(int), target);

    printf("\n%d\n",out[0]);
    printf("%d\n",out[1]);

    return 0;
}




2017年1月11日 星期三

OC筆記

Array

NSArray *Path = @[
                            @"/root/AppleInternal/Diags/Logs/Bonfire/",
                            @"/root/var/logs/Earthbound/",
                            @"/root/var/mobile/Media/FactoryLogs/LogCollector/Earthbound/"
                ];
//array調用
NSLog(@"%@",Path[1]);

class的宣告(位於.h頭文件)

@interface 類名 : NSObject{
@protected
}
@end
//example
@interface ASStudent : NSObject{
@protected //用protected聲明的變數,無法透過 . or -> 調用,只能夠過類所定義的方法調用。提升系統安全性。
    NSString *name; //宣告於NSString的方法都是實例方法都要使用指標。
    int age;
    NSString *sid;
}
-(NSString*) name;
-(void*) setName:(NSString*)aName;
-(int)age;
-(void)setAge:(int)aAge;
@end

class的實現(位於.m源文件)

@implementation
@end
@implementation
-(NSString*)name{
    return name;
}
-(NSString)setName:
@end

OC 函數宣告
OC 函數調用

字串處理

NSString *str = [[NSString alloc] initWithFormat:(nonnull NSString *), ...] //
int a = 5;
int b = 6;
NSString *str = [[NSString alloc] initWithFormat:@"%d + %d = %d",a,b,a+b];
[NSString stringWithString:(nonnull NSString *)], 用一個字串物件來建立另一個字串物件。返回一个auto-released的指向NSString的指针,所以不需要手工释放。
[NSString stringWithFormat:(nonnull NSString *), ...],

讀txt文件

函數stringWithContentsOfFile, 可將檔案讀近來變成字串。
NSString *txtPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/KernelPanicLog/SN.txt"];
NSString *SNData=[NSString stringWithContentsOfFile:txtPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",SNData);

創建資料夾

-(void)createDirForImage :(NSString *)dirName
{
    NSString *path;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path = [[paths objectAtIndex:0] stringByAppendingPathComponent:dirName];
    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:path])    //Does directory already exist?
    {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:path
                                       withIntermediateDirectories:NO
                                                        attributes:nil
                                                             error:&error])
        {
            NSLog(@"Create directory error: %@", error);
        }
    }
    
}



123214

OC筆記

Array

NSArray *Path = @[
                            @"/root/AppleInternal/Diags/Logs/Bonfire/",
                            @"/root/var/logs/Earthbound/",
                            @"/root/var/mobile/Media/FactoryLogs/LogCollector/Earthbound/"
                ];
//array調用
NSLog(@"%@",Path[1]);

class的宣告(位於.h頭文件)

@interface 類名 : NSObject{
@protected
}
@end
//example
@interface ASStudent : NSObject{
@protected //用protected聲明的變數,無法透過 . or -> 調用,只能夠過類所定義的方法調用。提升系統安全性。
    NSString *name; //宣告於NSString的方法都是實例方法都要使用指標。
    int age;
    NSString *sid;
}
-(NSString*) name;
-(void*) setName:(NSString*)aName;
-(int)age;
-(void)setAge:(int)aAge;
@end

class的實現(位於.m源文件)

@implementation
@end
@implementation
-(NSString*)name{
    return name;
}
-(NSString)setName:
@end

OC 函數宣告
OC 函數調用

字串處理

NSString *str = [[NSString alloc] initWithFormat:(nonnull NSString *), ...] //
int a = 5;
int b = 6;
NSString *str = [[NSString alloc] initWithFormat:@"%d + %d = %d",a,b,a+b];
[NSString stringWithString:(nonnull NSString *)], 用一個字串物件來建立另一個字串物件。返回一个auto-released的指向NSString的指针,所以不需要手工释放。
[NSString stringWithFormat:(nonnull NSString *), ...],

讀txt文件

函數stringWithContentsOfFile, 可將檔案讀近來變成字串。
NSString *txtPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/KernelPanicLog/SN.txt"];
NSString *SNData=[NSString stringWithContentsOfFile:txtPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",SNData);

創建資料夾

-(void)createDirForImage :(NSString *)dirName
{
    NSString *path;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path = [[paths objectAtIndex:0] stringByAppendingPathComponent:dirName];
    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:path])    //Does directory already exist?
    {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:path
                                       withIntermediateDirectories:NO
                                                        attributes:nil
                                                             error:&error])
        {
            NSLog(@"Create directory error: %@", error);
        }
    }
    
}



2017年1月10日 星期二

Led Driver 筆記

![1](media/1.png) —



PWM_duty_cycle_with_labe

Figure36是一個串接了256顆LED Driver的範例

2016年12月15日 星期四

OC筆記

OC筆記

Array

NSArray *Path = @[
                            @"/root/AppleInternal/Diags/Logs/Bonfire/",
                            @"/root/var/logs/Earthbound/",
                            @"/root/var/mobile/Media/FactoryLogs/LogCollector/Earthbound/"
                ];
//array調用
NSLog(@"%@",Path[1]);

class的宣告(位於.h頭文件)

@interface 類名 : NSObject{
@protected

}
@end

//example
@interface ASStudent : NSObject{
@protected //用protected聲明的變數,無法透過 . or -> 調用,只能夠過類所定義的方法調用。提升系統安全性。
    NSString *name; //宣告於NSString的方法都是實例方法都要使用指標。
    int age;
    NSString *sid;
}
-(NSString*) name;
-(void*) setName:(NSString*)aName;
-(int)age;
-(void)setAge:(int)aAge;
@end

class的實現(位於.m源文件)

@implementation
@end

@implementation
-(NSString*)name{
    return name;
}
-(NSString)setName:
@end

OC 函數宣告
OC 函數調用

字串處理

NSString *str = [[NSString alloc] initWithFormat:(nonnull NSString *), ...] //

int a = 5;
int b = 6;
NSString *str = [[NSString alloc] initWithFormat:@"%d + %d = %d",a,b,a+b];

[NSString stringWithString:(nonnull NSString *)], 用一個字串物件來建立另一個字串物件。返回一个auto-released的指向NSString的指针,所以不需要手工释放。
[NSString stringWithFormat:(nonnull NSString *), ...],

讀txt文件

函數stringWithContentsOfFile, 可將檔案讀近來變成字串。

NSString *txtPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/KernelPanicLog/SN.txt"];
NSString *SNData=[NSString stringWithContentsOfFile:txtPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",SNData);

創建資料夾

-(void)createDirForImage :(NSString *)dirName
{
    NSString *path;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    path = [[paths objectAtIndex:0] stringByAppendingPathComponent:dirName];
    NSError *error;
    if (![[NSFileManager defaultManager] fileExistsAtPath:path])    //Does directory already exist?
    {
        if (![[NSFileManager defaultManager] createDirectoryAtPath:path
                                       withIntermediateDirectories:NO
                                                        attributes:nil
                                                             error:&error])
        {
            NSLog(@"Create directory error: %@", error);
        }
    }
    
}

2016年5月29日 星期日

I2S (inter-IC sound)

 

I2s inter-IC sound,是飛利浦在1986年制定(1996年修訂)的數位音頻的傳輸標準(Digital Audio Interfaces),用於數字音頻訊號在系統內部元件之間傳輸。與I2C無關。

111

I2S 聲音訊號的傳輸,要注意以下幾件事情:

1.傳輸的對象(你總要知道誰傳給誰吧? 請看圖。)

2.解析度。(聲音儲存的精細度。常見16k)

3.聲音的取樣頻率。(44.1 kHz , 48kHz)

4.clock由誰發送。(取決於誰是master TX、RX皆可傳送)

113 112

5.I2S 如何定義信號。

 

6.I2S 操作模式。

 

節錄DH網站上的 網友 LukeLo 的「漫談數位音樂」
音樂CD的規格為什麼是44.1kHz、16Bits呢?關於44.1kHz這個數字的選取分為兩個層面。首先我們知道人耳的聆聽範圍是20Hz到 20kHz,根據Nyquist Functions,理論上我們只要用40kHz以上的取樣率就可以完整紀錄20kHz以下的訊號。那麼為什麼要用44.1kHz這個數字呢?那是因為在 CD發明前硬碟還很貴,所以主要數位音訊儲存媒體是錄影帶,用黑白來記錄0與1。而當時的錄影帶格式為每秒30張,而一張圖又可以分為490條線,每一條線又可以儲存三個取樣訊號,因此每秒有30*490*3=44100個取樣點,而為了研發的方便,CD也繼承了這個規格,這就是44.1kHz的由來。

 

Q:如果以16k的解析度錄製了一段WMV音樂,其容量為多少呢?

Ans:   5 x 44.1k x 16 x 2  kb

[LeetCode] Two sum (easy)

twosum Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each...