방문한 곳은 1 to 2로 치환하고 연결된 모든 섬의 count를 구해서 max sum을 구한다.
Runtime: 2 ms, faster than 100.00% of Java online submissions for Max Area of Island.
Memory Usage: 43.6 MB, less than 55.56% of Java online submissions for Max Area of Island.
Runtime: 7 ms, faster than 5.74% of Java online submissions for Power of Four.
Memory Usage: 34.3 MB, less than 6.67% of Java online submissions for Power of Four.
실제 코인의 이동이 아니라 코인이 없더라도 음수대로 값을 채움 ( 있는것처럼 가상의 코인을 움직인다 )
data를 채우는것은 inorder 순서로 적용
Runtime: 1 ms, faster than 87.01% of Java online submissions for Distribute Coins in Binary Tree.
Memory Usage: 37.7 MB, less than 100.00% of Java online submissions for Distribute Coins in Binary Tree.
Hi! I’m your first Markdown file in StackEdit. If you want to learn about StackEdit, you can read me. If you want to play with Markdown, you can edit me. Once you have finished with me, you can create new files by openi
1차 코드
49 ms 38.5 MB java
classSolution{public String removeDuplicates(String S){// "abbaca"if(S.length()<=1){return S;}char[] chars = S.toCharArray();
Stack<Character> stack =newStack<Character>();for(int i=0; i<chars.length; i++){if(!stack.isEmpty()){
Character cha = stack.peek();if(cha != null && cha == chars[i]){
stack.pop();continue;}}
stack.push(chars[i]);}char[] result =newchar[stack.size()];int index = stack.size()-1;while(!stack.isEmpty()){
Character last = stack.pop();
result[index--]= last;}returnnewString(result);}}
2차코드
자료구조 안쓰고 다시 한번더 풀어봐야겠다.
조건이 바로 앞뒤 2개의 character 값만 비교하는거라서 마지막 last index를 기억하는걸로
앞뒤가 서로 다를때 last index = currentIndex
앞뒤가 서로 같을때 find last index and check index ‘0’
Runtime: 4 ms, faster than 98.11% of Java online submissions for Remove All Adjacent Duplicates In String.
Memory Usage: 37.7 MB, less than 100.00% of Java online submissions for Remove All Adjacent Duplicates In String.
classSolution{public String removeDuplicates(String S){// "abbaca"if(S.length()<=1){return S;}char[] chars = S.toCharArray();int last =0;for(int i=1; i<chars.length; i++){if(last >=0&& chars[last]== chars[i]){// 같을때
chars[i]='0';
chars[last]='0';
last --;while(last >=0&& chars[last]=='0'){
last --;}}else{
last = i;}}
StringBuilder builder =newStringBuilder();for(int i=0; i<chars.length; i++){if(chars[i]!='0'){
builder.append(chars[i]);}}return builder.toString();}}
3차코드
Runtime: 3 ms, faster than 99.83% of Java online submissions for Remove All Adjacent Duplicates In String.
Memory Usage: 38.1 MB, less than 100.00% of Java online submissions for Remove All Adjacent Duplicates In String.
2차코드에서 StringBilder 제거
// "abbaca"if(S.length()<=1){return S;}char[] chars = S.toCharArray();int last =0;int resltLenth = chars.length;for(int i=1; i<chars.length; i++){if(last >=0&& chars[last]== chars[i]){// 같을때
chars[i]='0';
chars[last]='0';
last --;
resltLenth-=2;while(last >=0&& chars[last]=='0'){
last --;}}else{
last = i;}}char[] result =newchar[resltLenth];int resultIndex =0;for(int i=0; i<chars.length; i++){if(chars[i]!='0'){
result[resultIndex++]= chars[i];}}returnnewString(result);
Runtime: 1 ms, faster than 100.00% of Java online submissions for Can Place Flowers.
Memory Usage: 38.6 MB, less than 100.00% of Java online submissions for Can Place Flowers.