티스토리 뷰

 

 


문제

Given a string, find the length of the longest substring without repeating characters.

 

문자가 반복되지 않는 최대의 길이를 반환하라는 문제

 

 

예제 1

Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3.

 

 

예제 2

Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1.

 

 

예제 3

Input: "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

 

 

 

 

 

 

 

내 소스

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

class Solution {

    public int lengthOfLongestSubstring(String s) {

        HashMap<Integer, Character> hashmap = new HashMap<Integer, Character>();

        int[] cnt = new int[s.length()];

        int k = 0;

        int j = 0;

        int count = 0;

 

        for (int i = 0; i < s.length(); i++) {

            int l = i;

            while (l < s.length()) {

 

                if (hashmap.containsValue(s.charAt(l))) {

                    cnt[j] = count;

                    j = j + 1;

                    count = 0;

                    hashmap.clear();

                    break;

                }

 

                if (!hashmap.containsValue(s.charAt(l))) {

                    hashmap.put(l, s.charAt(l));

                    count = count + 1;

                    cnt[j] = count;

                }

                l = l + 1;

            }

 

        }

    }

}

Colored by Color Scripter

cs

 

 

 

 

이번에도 반복문 2개를 겹친 하드코딩을 했습니다 ㅠㅠ

조금 더 실용적인 코드를 짜신 분들은 알려주신다면 감사하겠습니다 ㅜ

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함