Try   HackMD

身為店仔大四快畢業的學生,好像沒有認真地寫code。
對於指標、陣列、樹使用上還不太熟悉,多停於紙上談兵。
繼大四上修資工的資結後,利用寒假期間寫題目紀錄一下。

  • 目標:
    • CPE通過3+題
  • 使用工具:
    • 資料結構與演算法 徐熊健 編著
    • LeetCode
    • 瘋狂程設

Day1:陣列

一組具有相同資料型態的有序集合(ordered set),在電腦的實體配置中通常存於連續的記憶體空間。
使用陣列名稱+註標index表示陣列內的一個元素。

  • 陣列名稱:記憶體的起始位置
  • 註標:自起始位置數起的第幾個位置
  1. 陣列的宣告:名稱+大小+資料型態
    ​​​​#define size 5 ​​​​//利用常數定義 增加可讀性 ​​​​main() ​​​​{ ​​​​ int lines[size],list[size]; ​​​​ int i; ​​​​ for(int i=0;i<size;i++){ ​​​​ lines[i]=i+1; ​​​​ list[size-i-1]=lines[i];//陣列從0開始,要多-1 ​​​​ } ​​​​ //lines[size]={1,2,3,4,5} ​​​​ //list[size]={5,4,3,2,1} ​​​​}

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  1. 間接參照
    若name[i]中依座號放學生姓名、rank[i]中依成績高低放座號,
    name[rank[i]] 得知成績第幾名的學生姓名。

1480.一維數組的運行總和.Running Sum of 1d Array

給定一個數組nums。將數組的運行和定義為 runningSum[i] = sum(nums[0]…nums[i])。

傳回 的運行總和nums。

範例1:

輸入: nums = [1,2,3,4]
輸出: [1,3,6,10]
解釋:運行總和如下:[1, 1+2, 1+2+3, 1+2+ 3+4 ]。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

malloc()動態配置記憶體空間

int x=5; int* p;

整數變數x的內容(value)為5;
*p是指向整數的指標 或 p是個位址/指標變數;p是個整數
p=&xp指向x:整數位址變數p 存放 變數x的位址

指標名稱:address
*指標名稱:內容value

//*Note: The returned array must be malloced, assume caller calls free(). //* // int* runningSum(int* nums, int numsSize, int* returnSize){//傳入: 指標 陣列大小 回傳陣列的大小 //int nums[numsSize]; //nums=malloc(sizeof(int));//動態配置實體位置 //nums本身就是一個 指向傳入陣列的 指標,不用重新分配空間 int* Arry=malloc(numsSize*sizeof(int)); //配置實體空間: 陣列大小*依資料型態所佔的空間 *returnSize=numsSize; //設定回傳陣列的大小 = 輸入的陣列大小 for(int i=0;i<numsSize;i++){ if(i!=0)Arry[i]=Arry[i-1]+nums[i]; else Arry[i]=nums[i]; } return Arry; }

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →


Day2:陣列

  1. 陣列的運算
    SelectionSort選擇排序
    在還沒排好的隊伍中找到最小值,丟到最左邊

    找最小值需要:
    第一回合:全部經過、遍歷N個步驟
    第二回合:N-1個步驟
    每回合再 加上丟到最左邊一次
    總共需要:N*(N+1)/2+N 次 
    O(n^2)

    ​​​​//由小到大排 ​​​​void swap(int *x,int *y){ ​​​​ int temp; ​​​​ temp=*x; ​​​​ *x = *y; ​​​​ *y = temp; ​​​​} ​​​​//互換 傳址 ( *x 是內容 ) ​​​​void SelectionSort(int data[],int datalen){ ​​​​ for(int i=0;i<datalen-1;i++)//datalen-1:最後一個可以不用比了 省下來 ​​​​ for(int j=i+1;j<datalen;j++) ​​​​ if(data[i]>data[j]) swap(&data[i],&data[j]); ​​​​} ​​​​//使用方式:呼叫SelectionSort(data,datalen);
  • 用指標傳址

宣告:void swap(int *p,int*y) *p 是內容
呼叫:swap(&data[i],&data[j]) &x 是位址

Day3:陣列

1672. Richest Customer Wealth

輸入m x n 的整數矩陣accounts
accounts[i][j]是客戶儲存的錢
回傳 最多財富的金額

範例1:

輸入: accounts = [[1,2,3],[3,2,1]]
輸出: 6
解釋:
1st customer has wealth = 1 + 2 + 3 = 6
2nd customer has wealth = 3 + 2 + 1 = 6
兩個客戶都被認為是最富有的,每個人的財富為 6,因此返回 6。