4 분 소요


image
I’ve started doing LeetCode. Of course, it’s free plan but, if I get interest I will buy the premium version. Today is the first time so I am doing Explore. I am writing blog simultaniously like in “The Social Network”.

What I’ve Learned (After today’s problems)

First, I’ve learned that I can use ArrayList in java. List<String> arr = new ArrayList<>().
Second, we can use knowledge operand to check if it’s odd or even. num & 1 has the same meaning as num % 2 and num >> 2 can be used as num / 2.
Third, Linked List is a list that has the number and pointer. So, we cannot know the full length of Linked List.
Fourth, by maximizing the usage of Linked List, we can use two pointers to check end of list and middle of list. When full length of list increases by 2, the middle value increases.

Disclaimer

My LeetCode is free plan. I am posting just the free algorithms for now on. Also, I am using JAVA to code.

Fizz Buzz

My solution

Actually, I didn’t know that List and Array was different in java. I really don’t have much of algorithm basics. I guess, I can develop through doing LeetCode.
For my excuse, I made a Fizz Buzz code using array easily. It was simple. But, the question didn’t want that. They wanted me to use List. So, i built it again.

import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<String> fizzBuzz(int n) {
    	List<String> inputArr = new ArrayList<>();
    	
    	for(int i = 1; i<=n; i++) {
    		if(i % 3 == 0 && i % 5==0)
    			inputArr.add("FizzBuzz");
    		else if(i % 3 == 0)
    			inputArr.add("Fizz");
    		else if(i % 5 == 0)
    			inputArr.add("Buzz");
    		else
    			inputArr.add(String.valueOf(i));
    	}
    	
    	for(String e1 : inputArr) {
    	 System.out.print(e1);
    	}
    	
    	return inputArr;
    	
    }
}

Solution

This works, but, runtime takes 23ms. The most propotion of submition has less than mine. The answer was to use boolean. How can I didn’t came up with boolean!
With a simple code of just making boolean divisibleBy3 = i % 3 ==0, it makes less.

1342. Number of Steps to Reduce a Number to Zero

My solution

Only limit of free plan on LeetCode is that I cannot use cloud saving and running. I need to open the Eclipse to use JAVA.
Number of Steps to Reduce a Number to Zero. Easy. Not a hard question. Divide in 2 when even, subtract 1 when odd.

class Solution {
    public int numberOfSteps(int num) {
    	int count = 0;
    	while(num != 0) {
    		if(num % 2 == 0)
            	num /=2;
            else
            	num -=1;
    		count++;
    	}
    	
        return count;
    }
}

Solution

Okay, it worked but Leetcode is offering to use shift operator to use less bits. Instead of num % 2, we can use num & 1. num & 1 is for checking the final integer if it’s 0 or 1. If it’s odd, num’s final integer will be 1 and if it’s even, final integer will be 0. So, we can use logical operands to check if it’s odd or even.
In the same way, we can use shift operands to divide 2. num >> 2 is same as num / 2. It was used on Raycaster with C.

876. Middle of The Linked List

My Solution

Okay, I didn’t know about linked list. I just thought that it will be same as array. But, there are few differences. Nodes are the box of number and pointing to next Node. So, we just know the number. There are no index, as a result no middle value. There are few ways to go through this.
I will use ArrayList to save the numbers in Linked List and count up when each work is done. But there are another problem. For given ListNode class. I really don’t know how to pick on value.

class ListNode {
	int val;
	ListNode next;
	
	ListNode() {}
	ListNode(int val) { 
		this.val = val; 
	}
	ListNode(int val, ListNode next) { 
		this.val = val;
		this.next = next; 
	}
	
}

Okay, int val is the first number and ListNode next is the next ListNode.

import java.util.ArrayList;

class ListNode {
	int val;
	ListNode next;
	
	ListNode() {}
	ListNode(int val) { 
		this.val = val; 
	}
	ListNode(int val, ListNode next) { 
		this.val = val;
		this.next = next; 
	}
	
}


class Solution {
    public ListNode middleNode(ListNode head) {
    	ArrayList<ListNode> arr = new ArrayList<>();
    	int count= 0;
    	while(head != null) {
    		arr.add(head);
    		head = head.next;
    		count++;
    	}
        return arr.get(count/2);
    }
}

Understanding the structure of ListNode was the key and using arraylist was the key also.

Solution

Using two pointers which are keep tracking the last node and middle node. Middle Nodes grows when the length of node increases by two.
length = 3, middle node index = 2
length = 4, middle node index = 2
length = 5, middle node index = 3

class Solution {
    public ListNode middleNode(ListNode head) {

    	ListNode middle = head;
    	ListNode end = head;
    	while(end != null && end.next != null) {
    		middle = middle.next;
    		end = end.next.next;
    	}
    	return middle;
    }
}

This is a great idea. I’ve learn something today.

카테고리:

업데이트: