演算法練習筆記
注意題目裡的線索
例如題目如果出現”寫一個在伺服器長時間重複運作的程式”,你可以想看看快取是不是可以派上用場寫出測試範例
要特別注意自己寫出來的範例是不是特例嘗試暴力解
優化
Check Permutation: Given two strings, write a method to decide if one is a permutation of the other. p.90
1 | def check_permutation(s1, s2): |
samples
- dad, bda
- abc, bca
- sss, sss
- sas, ssa
- abcd, abc
- bbba, bbbb
URLify: Write a method to replace all spaces in a string with ‘%20’. You may assume that the string has sufficient space at the end to hold the additional characters, and that you are given the “true” length of the string. (Note: If implementing in Java, please use a character array so that you can perform this operation in place.)
- 錯誤1:
忘記回傳值
Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palin-drome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.
1 | import copy |
錯誤1: 沒有考慮到大小寫問題
One Away: There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away.
1 | def one_edit(str_a, str_b): |