diff --git "a/problems/0001.344円270円244円346円225円260円344円271円213円345円222円214円.md" "b/problems/0001.344円270円244円346円225円260円344円271円213円345円222円214円.md" index 9571a7737b..459a66ad8c 100644 --- "a/problems/0001.344円270円244円346円225円260円344円271円213円345円222円214円.md" +++ "b/problems/0001.344円270円244円346円225円260円344円271円213円345円222円214円.md" @@ -274,6 +274,24 @@ class Solution { } } ``` +C#: +```csharp +public class Solution { + public int[] TwoSum(int[] nums, int target) { + Dictionary dic= new Dictionary(); + for(int i=0;i diff --git "a/problems/0019.345円210円240円351円231円244円351円223円276円350円241円250円347円232円204円345円200円222円346円225円260円347円254円254円N344円270円252円350円212円202円347円202円271円.md" "b/problems/0019.345円210円240円351円231円244円351円223円276円350円241円250円347円232円204円345円200円222円346円225円260円347円254円254円N344円270円252円350円212円202円347円202円271円.md" index b3030a81d1..c36900bcde 100644 --- "a/problems/0019.345円210円240円351円231円244円351円223円276円350円241円250円347円232円204円345円200円222円346円225円260円347円254円254円N344円270円252円350円212円202円347円202円271円.md" +++ "b/problems/0019.345円210円240円351円231円244円351円223円276円350円241円250円347円232円204円345円200円222円346円225円260円347円254円254円N344円270円252円350円212円202円347円202円271円.md" @@ -289,6 +289,28 @@ func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { return dummyHead.next } ``` - +Scala: +```scala +object Solution { + def removeNthFromEnd(head: ListNode, n: Int): ListNode = { + val dummy = new ListNode(-1, head) // 定义虚拟头节点 + var fast = head // 快指针从头开始走 + var slow = dummy // 慢指针从虚拟头开始头 + // 因为参数 n 是不可变量,所以不能使用 while(n>0){n-=1}的方式 + for (i <- 0 until n) { + fast = fast.next + } + // 快指针和满指针一起走,直到fast走到null + while (fast != null) { + slow = slow.next + fast = fast.next + } + // 删除slow的下一个节点 + slow.next = slow.next.next + // 返回虚拟头节点的下一个 + dummy.next + } +} +``` -----------------------
diff --git "a/problems/0024.344円270円244円344円270円244円344円272円244円346円215円242円351円223円276円350円241円250円344円270円255円347円232円204円350円212円202円347円202円271円.md" "b/problems/0024.344円270円244円344円270円244円344円272円244円346円215円242円351円223円276円350円241円250円344円270円255円347円232円204円350円212円202円347円202円271円.md" index ce75e0d79f..2289c22912 100644 --- "a/problems/0024.344円270円244円344円270円244円344円272円244円346円215円242円351円223円276円350円241円250円344円270円255円347円232円204円350円212円202円347円202円271円.md" +++ "b/problems/0024.344円270円244円344円270円244円344円272円244円346円215円242円351円223円276円350円241円250円344円270円255円347円232円204円350円212円202円347円202円271円.md" @@ -311,7 +311,29 @@ func swapPairs(_ head: ListNode?) -> ListNode? { return dummyHead.next } ``` - +Scala: +```scala +// 虚拟头节点 +object Solution { + def swapPairs(head: ListNode): ListNode = { + var dummy = new ListNode(0, head) // 虚拟头节点 + var pre = dummy + var cur = head + // 当pre的下一个和下下个都不为空,才进行两两转换 + while (pre.next != null && pre.next.next != null) { + var tmp: ListNode = cur.next.next // 缓存下一次要进行转换的第一个节点 + pre.next = cur.next // 步骤一 + cur.next.next = cur // 步骤二 + cur.next = tmp // 步骤三 + // 下面是准备下一轮的交换 + pre = cur + cur = tmp + } + // 最终返回dummy虚拟头节点的下一个,return可以省略 + dummy.next + } +} +``` -----------------------
diff --git "a/problems/0202.345円277円253円344円271円220円346円225円260円.md" "b/problems/0202.345円277円253円344円271円220円346円225円260円.md" index 741a735a5d..51a79aff96 100644 --- "a/problems/0202.345円277円253円344円271円220円346円225円260円.md" +++ "b/problems/0202.345円277円253円344円271円220円346円225円260円.md" @@ -385,5 +385,28 @@ bool isHappy(int n){ return bHappy; } ``` + +C#: +```csharp +public class Solution { + private int getSum(int n) { + int sum = 0; + //每位数的换算 + while (n> 0) { + sum += (n % 10) * (n % 10); + n /= 10; + } + return sum; + } + public bool IsHappy(int n) { + HashSet set = new HashSet(); + while(n != 1 && !set.Contains(n)) { //判断避免循环 + set.Add(n); + n = getSum(n); + } + return n == 1; + } +} +``` -----------------------
diff --git "a/problems/0206.347円277円273円350円275円254円351円223円276円350円241円250円.md" "b/problems/0206.347円277円273円350円275円254円351円223円276円350円241円250円.md" index 941928baae..25b16907e4 100644 --- "a/problems/0206.347円277円273円350円275円254円351円223円276円350円241円250円.md" +++ "b/problems/0206.347円277円273円350円275円254円351円223円276円350円241円250円.md" @@ -496,6 +496,40 @@ struct ListNode* reverseList(struct ListNode* head){ return reverse(NULL, head); } ``` +Scala: +双指针法: +```scala +object Solution { + def reverseList(head: ListNode): ListNode = { + var pre: ListNode = null + var cur = head + while (cur != null) { + var tmp = cur.next + cur.next = pre + pre = cur + cur = tmp + } + pre + } +} +``` +递归法: +```scala +object Solution { + def reverseList(head: ListNode): ListNode = { + reverse(null, head) + } + + def reverse(pre: ListNode, cur: ListNode): ListNode = { + if (cur == null) { + return pre // 如果当前cur为空,则返回pre + } + val tmp: ListNode = cur.next + cur.next = pre + reverse(cur, tmp) // 此时cur成为前一个节点,tmp是当前节点 + } +} +``` -----------------------
diff --git "a/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.md" "b/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.md" index 080166fd34..878b24662c 100644 --- "a/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.md" +++ "b/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.md" @@ -307,6 +307,25 @@ impl Solution { } } ``` + +C#: +```csharp + public bool IsAnagram(string s, string t) { + int sl=s.Length,tl=t.Length; + if(sl!=tl) return false; + int[] a = new int[26]; + for(int i = 0; i < sl; i++){ + a[s[i] - 'a']++; + a[t[i] - 'a']--; + } + foreach (int i in a) + { + if (i != 0) + return false; + } + return true; + } +``` ## 相关题目 * 383.赎金信 diff --git "a/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md" "b/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md" index 45f19b6ef2..7f8958d232 100644 --- "a/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md" +++ "b/problems/0349.344円270円244円344円270円252円346円225円260円347円273円204円347円232円204円344円272円244円351円233円206円.md" @@ -313,6 +313,24 @@ int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* re } ``` +C#: +```csharp + public int[] Intersection(int[] nums1, int[] nums2) { + if(nums1==null||nums1.Length==0||nums2==null||nums1.Length==0) + return new int[0]; //注意数组条件 + HashSet one = Insert(nums1); + HashSet two = Insert(nums2); + one.IntersectWith(two); + return one.ToArray(); + } + public HashSet Insert(int[] nums){ + HashSet one = new HashSet(); + foreach(int num in nums){ + one.Add(num); + } + return one; + } +``` ## 相关题目 * 350.两个数组的交集 II diff --git "a/problems/0383.350円265円216円351円207円221円344円277円241円.md" "b/problems/0383.350円265円216円351円207円221円344円277円241円.md" index 6177cc41a0..933a7c3111 100644 --- "a/problems/0383.350円265円216円351円207円221円344円277円241円.md" +++ "b/problems/0383.350円265円216円351円207円221円344円277円241円.md" @@ -363,5 +363,22 @@ impl Solution { } ``` +C#: +```csharp +public bool CanConstruct(string ransomNote, string magazine) { + if(ransomNote.Length> magazine.Length) return false; + int[] letters = new int[26]; + foreach(char c in magazine){ + letters[c-'a']++; + } + foreach(char c in ransomNote){ + letters[c-'a']--; + if(letters[c-'a']<0){ + return false; + } + } + return true; + } +``` -----------------------
diff --git "a/problems/0454.345円233円233円346円225円260円347円233円270円345円212円240円II.md" "b/problems/0454.345円233円233円346円225円260円347円233円270円345円212円240円II.md" index a6cd413b19..962fe7a541 100644 --- "a/problems/0454.345円233円233円346円225円260円347円233円270円345円212円240円II.md" +++ "b/problems/0454.345円233円233円346円225円260円347円233円270円345円212円240円II.md" @@ -318,5 +318,32 @@ impl Solution { } ``` +C#: +```csharp +public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { + Dictionary dic = new Dictionary(); + foreach(var i in nums1){ + foreach(var j in nums2){ + int sum = i + j; + if(dic.ContainsKey(sum)){ + dic[sum]++; + }else{ + dic.Add(sum, 1); + } + + } + } + int res = 0; + foreach(var a in nums3){ + foreach(var b in nums4){ + int sum = a+b; + if(dic.TryGetValue(-sum, out var result)){ + res += result; + } + } + } + return res; + } +``` -----------------------
diff --git "a/problems/0707.350円256円276円350円256円241円351円223円276円350円241円250円.md" "b/problems/0707.350円256円276円350円256円241円351円223円276円350円241円250円.md" index 37ce15adce..dcdb53f44e 100644 --- "a/problems/0707.350円256円276円350円256円241円351円223円276円350円241円250円.md" +++ "b/problems/0707.350円256円276円350円256円241円351円223円276円350円241円250円.md" @@ -1154,7 +1154,75 @@ class MyLinkedList { } ``` +Scala: +```scala +class ListNode(_x: Int = 0, _next: ListNode = null) { + var next: ListNode = _next + var x: Int = _x +} + +class MyLinkedList() { + + var size = 0 // 链表尺寸 + var dummy: ListNode = new ListNode(0) // 虚拟头节点 + + // 获取第index个节点的值 + def get(index: Int): Int = { + if (index < 0 || index>= size) { + return -1; + } + var cur = dummy + for (i <- 0 to index) { + cur = cur.next + } + cur.x // 返回cur的值 + } + + // 在链表最前面插入一个节点 + def addAtHead(`val`: Int) { + addAtIndex(0, `val`) + } + + // 在链表最后面插入一个节点 + def addAtTail(`val`: Int) { + addAtIndex(size, `val`) + } + + // 在第index个节点之前插入一个新节点 + // 如果index等于链表长度,则说明新插入的节点是尾巴 + // 如果index等于0,则说明新插入的节点是头 + // 如果index>链表长度,则说明为空 + def addAtIndex(index: Int, `val`: Int) { + if (index> size) { + return + } + var loc = index // 因为参数index是val不可变类型,所以需要赋值给一个可变类型 + if (index < 0) { + loc = 0 + } + size += 1 //链表尺寸+1 + var pre = dummy + for (i <- 0 until loc) { + pre = pre.next + } + val node: ListNode = new ListNode(`val`, pre.next) + pre.next = node + } + // 删除第index个节点 + def deleteAtIndex(index: Int) { + if (index < 0 || index>= size) { + return + } + size -= 1 + var pre = dummy + for (i <- 0 until index) { + pre = pre.next + } + pre.next = pre.next.next + } +} +``` ----------------------- diff --git "a/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" "b/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" index 2e7226deeb..0a38cc3309 100644 --- "a/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" +++ "b/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" @@ -317,7 +317,55 @@ ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { } ``` - +Scala: +```scala +object Solution { + def getIntersectionNode(headA: ListNode, headB: ListNode): ListNode = { + var lenA = 0 // headA链表的长度 + var lenB = 0 // headB链表的长度 + var tmp = headA // 临时变量 + // 统计headA的长度 + while (tmp != null) { + lenA += 1; + tmp = tmp.next + } + // 统计headB的长度 + tmp = headB // 临时变量赋值给headB + while (tmp != null) { + lenB += 1 + tmp = tmp.next + } + // 因为传递过来的参数是不可变量,所以需要重新定义 + var listA = headA + var listB = headB + // 两个链表的长度差 + // 如果gap>0,lenA>lenB,headA(listA)链表往前移动gap步 + // 如果gap<0,lena 0) { + // 因为不可以i-=1,所以可以使用for + for (i <- 0 until gap) { + listA = listA.next // 链表headA(listA) 移动 + } + } else { + gap = math.abs(gap) // 此刻gap为负值,取绝对值 + for (i <- 0 until gap) { + listB = listB.next + } + } + // 现在两个链表同时往前走,如果相等则返回 + while (listA != null && listB != null) { + if (listA == listB) { + return listA + } + listA = listA.next + listB = listB.next + } + // 如果链表没有相交则返回null,return可以省略 + null + } +} +``` -----------------------

AltStyle によって変換されたページ (->オリジナル) /