int [] array = new int[5]; Console.WriteLine(array[0]); // 輸出:0 陣列的元素會初始化為整數的元素類型預設值 0 。
2.二維陣列
1 2
int [,] array = new int[4,2]; Console.WriteLine(array[1,1]); // // 輸出:0 陣列的元素會初始化為整數的元素類型預設值 0 。
陣列初始化
1.一維陣列
1 2
int [] array = new int[]{1,2,3,4,5}; Console.WriteLine(array[0]); // 輸出:1
2.二維陣列
1 2
int [,] array = new int[4,2]{{1,2},{3,4},{7,5},{4,8}}; // 在宣告時如果有初始值後面就必須要填滿那些範圍的元素 Console.WriteLine(array[2,1]); // 輸出5
3.不規則陣列 感覺很少用到
1 2 3 4 5
int[][] array = new int[3][]; // 宣告時必須int[][] 而不是 int[,] array[0] = new int[]{1,3}; array[1] = new int[]{5,3,3}; array[2] = new int[]{1,31,5}; Console.Write(array[2][1]);//輸出 31
4.隱含型別陣列 用var開頭,裡面的元素符合同一個型態即可
1 2
var array = new [] {"hi!",null,"world"}; Console.Write(array[2]);//輸出 world
遍歷陣列方式
1.使用for,如果處理多維較為方便,且比較直覺
1 2 3 4
int [] array = new int[]{1,2,3,4,5}; for(int i=0;i<array.Length;i++){ Console.Write(array[i]); } // 輸出:12345
2.foreach,如果是單維想簡潔些可以用
1 2 3 4
int [] array = new int[]{1,2,3,4,5}; foreach(int num in array){ Console.Write(num); } // 輸出:12345
把陣列傳入函式
1 2 3 4 5 6 7 8 9 10 11 12 13 14
using System; public class Program { public static void printArray(int [] arr){ for(int i=0;i<arr.Length;i++){ Console.Write(arr[i]); } } public static void Main() { int [] array = new int[]{1,2,3,4,5}; printArray(array); // 輸出12345 } }
using System; public class Program { public static int accumulate(int n){ if(n==1){ return 1; }else if(n >=2 ){ return n + accumulate(n-1); } return 0; } public static void Main() { Console.WriteLine("1+2+3..+10=" + accumulate(10)); // 1+2+3..+10=55 } }
let x = 10 { let x = 2 console.log(x) // 2 } console.log(x) // 10
let,有自己的區塊作用域,不會蓋掉底下宣告的變數
1 2 3 4 5 6
var x = 10 for(var i=0;i<5;i++){ var x = 5 console.log(x) //5 } console.log(x) // 5
var,沒有自己的區塊作用域,會蓋掉底下宣告的變數
1
const x = 10;
const用來宣告常數不會被改變,不過如果是物件,仍然可以使用提供的方法去改變裡面的數值。
結論 如果沒有必要需求還是用let或const好,因為不會往下覆之後宣告的變數。
2. arrow function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<button>按鈕</button> <script> // 使用 addEventListener 監聽事件 var button = document.querySelector('button'); var fn_arrow = () => { // 建立 function 時 this 指向 Window console.log(this.constructor.name); // 執行 function 時 this 指向 Window }; var fn = function(){ // 建立 function 時 this 指向 Window console.log(this.constructor.name); // 執行 function 時 this 指向 HTMLButtonElement };
//節點 public class LinkNode { public int val; public LinkNode next; public LinkNode(int val,LinkNode next){ this.val = val; this.next = next; } public LinkNode(int val){ this.val = val; this.next = null; } };
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
The number of nodes in each linked list is in the range [1, 100].
0 <= Node.val <= 9
It is guaranteed that the list represents a number that does not have leading zeros.
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int val=0, ListNode next=null) { * this.val = val; * this.next = next; * } * } */
public class Solution { public ListNode AddTwoNumbers(ListNode l1, ListNode l2) { ListNode currentL1=l1,currentL2=l2; ListNode resultListNode = new ListNode(); ListNode resultList = resultListNode; // 為什麼可以儲存? Boolean isAddOne = false; while(true){ int sum = 0; if(currentL1!= null){ sum += currentL1.val; currentL1 = currentL1.next; } if(currentL2!= null){ sum += currentL2.val; currentL2 = currentL2.next; } if(isAddOne){ sum+=1; } if(sum >= 10){ isAddOne = true; sum = sum%10; }else{ isAddOne = false; } resultListNode.next = new ListNode(sum); resultListNode = resultListNode.next; // ?? 因C#傳址可影響到原本的resultList if(currentL1 == null && currentL2 == null && isAddOne == false){ break; } } return resultList.next; } }
Runtime: 96 ms, faster than 97.02% of C# online submissions for Add Two Numbers. Memory Usage: 28.2 MB, less than 82.24% of C# online submissions for Add Two Numbers.
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6 Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6 Output: [0,1]
Constraints:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
Only one valid answer exists.
Follow-up: Can you come up with an algorithm that is less than O(n^2) time complexity?
C#解答1
1 2 3 4 5 6 7 8 9 10 11 12 13
public class Solution { public int[] TwoSum(int[] nums, int target) { for (int i = 0; i < nums.Length - 1 ; i++) { for(int j=i+1;j < nums.Length;j++){ if(nums[i]+nums[j] == target){ return new int[] { i,j }; } } } return new int[]{}; } }
Runtime: 316 ms, faster than 51.22% of C# online submissions for Two Sum. Memory Usage: 32.1 MB, less than 96.22% of C# online submissions for Two Sum.
Runtime: 116 ms, faster than 35.30% of JavaScript online submissions for Two Sum. Memory Usage: 39.2 MB, less than 93.93% of JavaScript online submissions for Two Sum.