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

Commit2416657

Browse files
authored
Merge pull requestneetcode-gh#2030 from saip7795/sp/palindrome-partitioning
Create : 0131-Palindrome-Partitioning.rb
2 parents3ea0fd8 +cf41657 commit2416657

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎ruby/0131-palindrome-partitioning.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
defpartition(s)
2+
@result=[]
3+
@part=[]
4+
@s=s
5+
6+
defdfs(i)
7+
ifi >=@s.length
8+
@result.append(@part.dup())
9+
return
10+
end
11+
(i..@s.length-1).eachdo |j|
12+
ifis_palindrome(@s,i,j)
13+
@part.append(@s[i..j])
14+
dfs(j+1)
15+
@part.pop()
16+
end
17+
end
18+
end
19+
20+
dfs(0)
21+
22+
return@result
23+
end
24+
25+
26+
defis_palindrome(s,i,j)
27+
whilei<j
28+
returnfalseifs[i] !=s[j]
29+
i +=1
30+
j -=1
31+
end
32+
returntrue
33+
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp