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.