Skip to content

Commit 02adefc

Browse files
authored
Merge pull request neetcode-gh#2209 from Mohammed785/main
Create: 0021-merge-two-sorted-lists.rs, 0682-baseball-game, 1930-unique-length-3-palindromic-subsequences
2 parents 173d76e + 10fbabe commit 02adefc

8 files changed

+153
-0
lines changed

Diff for: csharp/0682-baseball-game.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Solution {
2+
public int CalPoints(string[] operations) {
3+
List<int> stack = new();
4+
5+
foreach(var operation in operations)
6+
{
7+
switch(operation)
8+
{
9+
case "D":
10+
stack.Add(stack[stack.Count()-1]*2);
11+
break;
12+
case "+":
13+
stack.Add(stack[stack.Count()-1] + stack[stack.Count()-2]);
14+
break;
15+
case "C":
16+
stack.RemoveAt(stack.Count()-1);
17+
break;
18+
default:
19+
stack.Add(int.Parse(operation));
20+
break;
21+
}
22+
}
23+
24+
return stack.Sum();
25+
}
26+
}

Diff for: javascript/0682-baseball-game.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string[]} operations
3+
* @return {number}
4+
*/
5+
var calPoints = function (operations) {
6+
const stack = [];
7+
for(const op of operations){
8+
if(op==="+"){
9+
stack.push(stack[stack.length - 1] + stack[stack.length - 2]);
10+
}else if(op==="C"){
11+
stack.pop()
12+
}else if(op==="D"){
13+
stack.push(stack[stack.length - 1] * 2);
14+
}else{
15+
stack.push(parseInt(op))
16+
}
17+
}
18+
return stack.reduce((prev,curr)=>prev+curr,0)
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var countPalindromicSubsequence = function (s) {
6+
let count = 0;
7+
let chars = new Set(s);
8+
for(const char of chars){
9+
let first = s.indexOf(char),last = s.lastIndexOf(char);
10+
count += new Set(s.slice(first + 1, last)).size;
11+
}
12+
return count;
13+
};
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def countPalindromicSubsequence(self, s: str) -> int:
3+
count = 0
4+
chars = set(s)
5+
for char in chars:
6+
first,last = s.find(char),s.rfind(char)
7+
count += len(set(s[first+1:last]))
8+
return count

Diff for: rust/0021-merge-two-sorted-lists.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn merge_two_lists(list1: Option<Box<ListNode>>, list2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
3+
match (list1,list2) {
4+
(Some(list1),None)=> Some(list1),
5+
(None,Some(list2))=>Some(list2),
6+
(None,None)=>None,
7+
(Some(l1),Some(l2))=>{
8+
if l1.val<l2.val {
9+
return Some(Box::new(ListNode{val:l1.val,next:Solution::merge_two_lists(l1.next,Some(l2))}));
10+
}else {
11+
return Some(Box::new(ListNode { val: l2.val, next: Solution::merge_two_lists(Some(l1),l2.next) }))
12+
}
13+
}
14+
}
15+
}
16+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::iter::FromIterator;
2+
3+
impl Solution {
4+
pub fn count_palindromic_subsequence(s: String) -> i32 {
5+
let mut result = 0;
6+
let mut ranges: [(i32, i32); 26] = [(-1, -1); 26];
7+
8+
for (i, c) in s.chars().enumerate() {
9+
let ix = Solution::char_to_index(c) as usize;
10+
if ranges[ix].0 == -1 {
11+
ranges[ix].0 = i as i32;
12+
}
13+
if i as i32 > ranges[ix].1 {
14+
ranges[ix].1 = i as i32;
15+
}
16+
}
17+
18+
for range in ranges {
19+
if range.1 > range.0 {
20+
let mut set: u32 = 0;
21+
for c in s[range.0 as usize + 1..range.1 as usize].chars() {
22+
set |= 1 << Solution::char_to_index(c);
23+
}
24+
result += set.count_ones() as i32;
25+
}
26+
}
27+
28+
result
29+
}
30+
31+
pub fn char_to_index(c: char) -> u32 {
32+
c as u32 - 'a' as u32
33+
}
34+
}

Diff for: swift/0682-baseball-game.swift

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
func calPoints(_ operations: [String]) -> Int {
3+
var scoreArray = [Int]()
4+
for i in 0..<operations.count {
5+
if operations[i] == "C" {
6+
scoreArray.removeLast()
7+
} else if operations[i] == "D" {
8+
scoreArray.append(scoreArray.last! * 2)
9+
} else if operations[i] == "+" {
10+
scoreArray.append(scoreArray[scoreArray.count - 2] + scoreArray[scoreArray.count - 1])
11+
} else {
12+
scoreArray.append(Int(operations[i])!)
13+
}
14+
}
15+
return scoreArray.isEmpty ? 0 : scoreArray.reduce(0, +)
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function countPalindromicSubsequence(s: string): number {
2+
const alphabets = 'abcdefghijklmnopqrstuvwxyz';
3+
4+
const N = alphabets.length;
5+
let count = 0;
6+
7+
for (let i = 0; i < N; i += 1) {
8+
const ch = alphabets[i];
9+
const left = s.indexOf(ch);
10+
const right = s.lastIndexOf(ch);
11+
if (left < right) {
12+
for (const alpha of alphabets) {
13+
const mid = s.indexOf(alpha, left + 1);
14+
if (mid !== -1 && mid < right) count += 1;
15+
}
16+
}
17+
}
18+
19+
return count;
20+
}

0 commit comments

Comments
 (0)