## 문제

(Logest Common Prefix)[https://leetcode.com/problems/longest-common-prefix/submissions/]

## 작성코드

```java

class Solution {

    public String longestCommonPrefix(String[] strs) {

        if (strs.length == 0) { return "";}

        System.out.print(strs[0]);

        String commonPrefix = "";

        

        for (int i = 0; i < strs[0].length(); i++) {

            for (int k = 0; k < strs.length - 1; k++) {

                if (strs[k].length() <= i || strs[k+1].length() <= i) {

                    return commonPrefix;

                } else if (strs[k].charAt(i) != strs[k+1].charAt(i) ) {

                    return commonPrefix;

                }

            }    

            commonPrefix += strs[strs.length - 1].charAt(i);

        }

        return commonPrefix;

    }

}


```

## 배운점

- indexOf() 를 사용하면 일치하는 문자열의 첫 시작을 알 수 있다.

- 첫 문자열을 기본으로 틀릴때마다 뒤에 길이를 1씩 줄임으로써 비교하는 것이 좋다.

'코테 > LeetCode' 카테고리의 다른 글

<LeetCode>04_13_Roman To Integer  (0) 2021.02.09
<LeetCode>03_09_Palindrome Number  (0) 2021.02.09
<LeetCode>02_07_ReverseInteger  (0) 2021.02.09
<LeetCode>01_01_TwoSum  (0) 2021.02.09

문제

Roman To Integer

작성코드

public class Solution {
    public int RomanToInt(string s) {
        int answer = 0;
        char pre = '\0';
        foreach(char c in s)
        {
            switch(c)
            {
                case 'I':
                    answer+=1;
                    break;
                case 'V':
                    if (pre == 'I')
                    {
                        answer+=3;
                    }
                    else
                    {
                        answer+=5;
                    }
                    break;
                case 'X':
                    if (pre == 'I')
                    {
                        answer+=8;
                    }
                    else
                    {
                        answer+=10;
                    }
                    break;
                case 'L':
                    if (pre == 'X')
                    {
                        answer+=30;
                    }
                    else
                    {
                        answer+=50;
                    }
                    break;
                case 'C':
                    if (pre == 'X')
                    {
                        answer+=80;
                    }
                    else
                    {
                        answer+=100;
                    }
                    break;
                case 'D':
                    if (pre == 'C')
                    {
                        answer+=300;
                    }
                    else
                    {
                        answer+=500;
                    }
                    break;
                case 'M':
                    if (pre == 'C')
                    {
                        answer+=800;
                    }
                    else
                    {
                        answer+=1000;
                    }
                    break;
            }
            pre = c;
        }

        return answer;
    }
}

배운점

  • 이전 자리의 문자를 비교할 때는 a[i] , a[i+1] 형태의 방식을 사용하자.

'코테 > LeetCode' 카테고리의 다른 글

<LeetCode>14_Longest Common Prefix  (0) 2021.02.20
<LeetCode>03_09_Palindrome Number  (0) 2021.02.09
<LeetCode>02_07_ReverseInteger  (0) 2021.02.09
<LeetCode>01_01_TwoSum  (0) 2021.02.09

문제

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.

Example 1:

Input: x = 121
Output: true

Example 2:

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Example 4:

Input: x = -101
Output: false

작성코드

public class Solution {
    public bool IsPalindrome(int x) {

        if (x < 0)
        {
            return false;
        }

        int leng = 0;
        int o = x;
        Stack<int> stack = new Stack<int>();

        while(x > 0)
        {
            stack.Push(x%10);
            x /= 10;
            leng++;
        }

        for (int i = 0; i < leng/2; i++)
        {
            if (o%10 != stack.Pop())
            {
                return false;
            }

            o /= 10;
        }

        return true;
    }
}

배운점

  • reverseNumber = reverseNumber * 10 + (x % 10); 정수역순은 해당 방식을 사용하자.

'코테 > LeetCode' 카테고리의 다른 글

<LeetCode>14_Longest Common Prefix  (0) 2021.02.20
<LeetCode>04_13_Roman To Integer  (0) 2021.02.09
<LeetCode>02_07_ReverseInteger  (0) 2021.02.09
<LeetCode>01_01_TwoSum  (0) 2021.02.09

문제

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31 - 1], then return 0.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

Example 1:

Input: x = 123
Output: 321

Example 2:

Input: x = -123
Output: -321

Example 3:

Input: x = 120
Output: 21

Example 4:

Input: x = 0
Output: 0

작성코드

public class Solution {
    public int Reverse(int x) {
        string a = "";
        string c = "";

        if  (x > 0)
        {
            a = x.ToString();
        }
        else 
        {
            c += "-";
            a = (x*-1l).ToString();
        }

        string b = "";
        string answer = "";

        if (a.Length%2 != 0)
        {
            answer = answer + a[(a.Length/2)];
        }

        for (int i = 0; i<(a.Length/2); i++)
        {
            b = a[i] + b;
            c = c + a[a.Length-1-i];
        }


        if ((2147483647 < long.Parse(c+answer+b))||(long.Parse(c+answer+b) < -2147483648))
        {
            return 0;
        }

        return int.Parse(c+answer+b);
    }
}

배운점

  • string으로 변환해 반복문 절반 줄이는 것보다 %를 이용한 반복이 훨씬 효율적.
  • int32에서 벗어난 수는 * -1이 통하지 않아서, -1l로 써야한다.

'코테 > LeetCode' 카테고리의 다른 글

<LeetCode>14_Longest Common Prefix  (0) 2021.02.20
<LeetCode>04_13_Roman To Integer  (0) 2021.02.09
<LeetCode>03_09_Palindrome Number  (0) 2021.02.09
<LeetCode>01_01_TwoSum  (0) 2021.02.09

문제

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].

작성코드

public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        int leng = nums.Length;
        int[] answer = new int[2];

        for (int i = 0; i < leng; i++)
        {
            for (int k = i + 1; k < leng; k++)
            {
                if (nums[i]+nums[k] == target)
                {
                    answer[0] = (i);
                    answer[1] = (k);
                    return answer;
                }
            }
        }

        return answer;
    }
}

배운점

  • return null 보다는 return Array.Empty<int>()를 사용하는 것이 RunTime에 이득.
  • nums.Length 를 변수에 담아 사용하는 것보다 for문 내에서 바로 사용하는 것이 RunTime이득.
  • Count보다 Length가 RunTime 이득.

'코테 > LeetCode' 카테고리의 다른 글

<LeetCode>14_Longest Common Prefix  (0) 2021.02.20
<LeetCode>04_13_Roman To Integer  (0) 2021.02.09
<LeetCode>03_09_Palindrome Number  (0) 2021.02.09
<LeetCode>02_07_ReverseInteger  (0) 2021.02.09

+ Recent posts

let textNodes = document.querySelectorAll("div.tt_article_useless_p_margin.contents_style > *:not(figure):not(pre)"); textNodes.forEach(function(a) { a.innerHTML = a.innerHTML.replace(/`(.*?)`/g, '$1'); });