-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[minji-go] Week 2 #732
[minji-go] Week 2 #732
Changes from 3 commits
5439322
ba72383
a128555
f05da5c
55f640c
1892c98
79d0f15
895e6b2
a9de6a6
93d395f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
Problem: https://leetcode.com/problems/climbing-stairs/ | ||
Description: how many distinct ways can you climb to the top, if you can either climb 1 or 2 steps | ||
Concept: Dynamic Programming, Memoization, Recursion, Math, Array, Iterator, Combinatorics ... | ||
Time Complexity: O(n), Runtime: 0ms | ||
Space Complexity: O(1), Memory: 40.51MB | ||
*/ | ||
class Solution { | ||
public int climbStairs(int n) { | ||
int step1=1, step2=2; | ||
for(int i=3; i<n; i++){ | ||
int step3=step1+step2; | ||
step1=step2; | ||
step2=step3; | ||
} | ||
return n==1?step1:step2; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
Problem: https://leetcode.com/problems/valid-anagram/ | ||
Description: return true if one string is an anagram of the other, one formed by rearranging the letters of the other | ||
Concept:String, Hash Table, Sorting, Array, Counting, String Matching, Ordered Map, Ordered Set, Hash Function ... | ||
Time Complexity: O(n), Runtime: 27ms | ||
Space Complexity: O(n), Memory: 43.11MB | ||
*/ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class Solution { | ||
public boolean isAnagram(String s, String t) { | ||
if(s.length() != t.length()) return false; | ||
|
||
Map<Character, Integer> count = new HashMap<>(); | ||
for(int i=0; i<s.length(); i++){ | ||
count.put(s.charAt(i), count.getOrDefault(s.charAt(i), 0)+1); | ||
count.put(t.charAt(i), count.getOrDefault(t.charAt(i), 0)-1); | ||
} | ||
for(Character key : count.keySet()){ | ||
if(count.get(key)!=0) return false; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
제가 풀이한 방식과 달라서 흥미롭게 봤습니다😀 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 습관적으로 사용하던걸 많이 되짚어볼 수 있었네요. 감사합니다👍 |
||
return true; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basecase
n이 1인 경우
를 삼항연산자로 처리하는 것보다얼리리턴으로 반환하는 것이 불필요한 연산을 줄이고 가독성이 좋을 것 같아요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코드줄 수를 줄이면 가독성이 높아진다고 생각했었던거 같은데,
n이 1인 경우를 얼리 리턴하면 오히려 의미파악이 쉬워지겠네요!