//節點 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.