Skip to content

Commit

Permalink
leetcode: Add 0647
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Sep 25, 2024
1 parent 3318c62 commit bb138e3
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/leetcode/0322.coin-change/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

def coinChange(coins: List[int], amount: int) -> int:
def dp(n):
# 基本情况
INIT_COUNT = 2 ** 10
# 基本常量
INITIAL_COUNT = 2 ** 30
INVALID_COUNT = -1
# 初始条件
if n == 0:
# 恰好兑换完
return 0
if n < 0:
# 说明没有合适的币值去完成兑换
return INVALID_COUNT
min_exchange_count = INIT_COUNT

# 初始的最小兑换次数要足够大
min_exchange_count = INITIAL_COUNT

# 依次遍历每一种币值, 并使用其中的币值换一次
# 找出最小的兑换次数
Expand All @@ -20,7 +25,7 @@ def dp(n):
min_exchange_count = min(min_exchange_count, exchange_count)

# 最后返回最小值
if min_exchange_count == INIT_COUNT:
if min_exchange_count == INITIAL_COUNT:
return INVALID_COUNT
else:
return min_exchange_count
Expand Down
7 changes: 7 additions & 0 deletions src/leetcode/0647.palindromic-substrings/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "lc-0647-palindromic-substrings"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
4 changes: 4 additions & 0 deletions src/leetcode/0647.palindromic-substrings/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#

[问题描述](https://leetcode.com/problems/)
45 changes: 45 additions & 0 deletions src/leetcode/0647.palindromic-substrings/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3

def countSubstrings(s: str) -> int:
# 回文字符串的一个特性是中间对齐, 我们可以遍历所有字符, 以它为中间, 尝试
# 向左右两侧扩展, 检查它们是否相等, 如果相等, 就说明这个 substring 是回文的.

palindrome_count = 0

def twoSideEquals(s: str, left: int, right: int) -> bool:
# 先检查两个索引值是否有效, 再检查它们指向的字符是否相等
if left >= 0 and right < len(s) and s[left] == s[right]:
return True
else:
return False

# middle 是 substring 的中间节点
for middle in range(len(s)):
# 如果 substring 中包含奇数个字符, 那就以 (middle) 作为中间对齐节点
left = middle
right = middle
while twoSideEquals(s, left, right):
palindrome_count += 1
left -= 1
right += 1

# 如果 substring 中包含偶数个字符, 那就以 (middle, middle+1) 作为中间对齐节点
left = middle
right = middle + 1
while twoSideEquals(s, left, right):
palindrome_count += 1
left -= 1
right += 1

# 返回结果
return palindrome_count

def main():
s1 = "abc"
assert(countSubstrings(s1) == 3)

s2 = "aaa"
assert(countSubstrings(s2) == 6)

if __name__ == "__main__":
main()
27 changes: 27 additions & 0 deletions src/leetcode/0647.palindromic-substrings/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

pub fn solution1() {
todo!();
}

pub type SolutionFn = fn();

fn check_solution(_func: SolutionFn) {
todo!();
}

fn main() {
check_solution(solution1);
}

#[cfg(test)]
mod tests {
use super::{check_solution, solution1};

#[test]
fn test_solution1() {
check_solution(solution1);
}
}

0 comments on commit bb138e3

Please sign in to comment.