Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit02adefc

Browse files
authored
Merge pull requestneetcode-gh#2209 from Mohammed785/main
Create: 0021-merge-two-sorted-lists.rs, 0682-baseball-game, 1930-unique-length-3-palindromic-subsequences
2 parents173d76e +10fbabe commit02adefc

8 files changed

+153
-0
lines changed

‎csharp/0682-baseball-game.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
publicclassSolution{
2+
publicintCalPoints(string[]operations){
3+
List<int>stack=new();
4+
5+
foreach(varoperationinoperations)
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+
returnstack.Sum();
25+
}
26+
}

‎javascript/0682-baseball-game.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
*@param {string[]} operations
3+
*@return {number}
4+
*/
5+
varcalPoints=function(operations){
6+
conststack=[];
7+
for(constopofoperations){
8+
if(op==="+"){
9+
stack.push(stack[stack.length-1]+stack[stack.length-2]);
10+
}elseif(op==="C"){
11+
stack.pop()
12+
}elseif(op==="D"){
13+
stack.push(stack[stack.length-1]*2);
14+
}else{
15+
stack.push(parseInt(op))
16+
}
17+
}
18+
returnstack.reduce((prev,curr)=>prev+curr,0)
19+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
*@param {string} s
3+
*@return {number}
4+
*/
5+
varcountPalindromicSubsequence=function(s){
6+
letcount=0;
7+
letchars=newSet(s);
8+
for(constcharofchars){
9+
letfirst=s.indexOf(char),last=s.lastIndexOf(char);
10+
count+=newSet(s.slice(first+1,last)).size;
11+
}
12+
returncount;
13+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
classSolution:
2+
defcountPalindromicSubsequence(self,s:str)->int:
3+
count=0
4+
chars=set(s)
5+
forcharinchars:
6+
first,last=s.find(char),s.rfind(char)
7+
count+=len(set(s[first+1:last]))
8+
returncount

‎rust/0021-merge-two-sorted-lists.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
implSolution{
2+
pubfnmerge_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+
returnSome(Box::new(ListNode{val:l1.val,next:Solution::merge_two_lists(l1.next,Some(l2))}));
10+
}else{
11+
returnSome(Box::new(ListNode{val: l2.val,next:Solution::merge_two_lists(Some(l1),l2.next)}))
12+
}
13+
}
14+
}
15+
}
16+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::iter::FromIterator;
2+
3+
implSolution{
4+
pubfncount_palindromic_subsequence(s:String) ->i32{
5+
letmut result =0;
6+
letmut ranges:[(i32,i32);26] =[(-1, -1);26];
7+
8+
for(i, c)in s.chars().enumerate(){
9+
let ix =Solution::char_to_index(c)asusize;
10+
if ranges[ix].0 == -1{
11+
ranges[ix].0 = iasi32;
12+
}
13+
if iasi32 > ranges[ix].1{
14+
ranges[ix].1 = iasi32;
15+
}
16+
}
17+
18+
for rangein ranges{
19+
if range.1 > range.0{
20+
letmut set:u32 =0;
21+
for cin s[range.0asusize +1..range.1asusize].chars(){
22+
set |=1 <<Solution::char_to_index(c);
23+
}
24+
result += set.count_ones()asi32;
25+
}
26+
}
27+
28+
result
29+
}
30+
31+
pubfnchar_to_index(c:char) ->u32{
32+
casu32 -'a'asu32
33+
}
34+
}

‎swift/0682-baseball-game.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
classSolution{
2+
func calPoints(_ operations:[String])->Int{
3+
varscoreArray=[Int]()
4+
foriin0..<operations.count{
5+
ifoperations[i]=="C"{
6+
scoreArray.removeLast()
7+
}elseifoperations[i]=="D"{
8+
scoreArray.append(scoreArray.last!*2)
9+
}elseifoperations[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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
functioncountPalindromicSubsequence(s:string):number{
2+
constalphabets='abcdefghijklmnopqrstuvwxyz';
3+
4+
constN=alphabets.length;
5+
letcount=0;
6+
7+
for(leti=0;i<N;i+=1){
8+
constch=alphabets[i];
9+
constleft=s.indexOf(ch);
10+
constright=s.lastIndexOf(ch);
11+
if(left<right){
12+
for(constalphaofalphabets){
13+
constmid=s.indexOf(alpha,left+1);
14+
if(mid!==-1&&mid<right)count+=1;
15+
}
16+
}
17+
}
18+
19+
returncount;
20+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp