알고리즘/LeetCode(6)
-
[LeetCode] [C++] Reverse Integer
1. problem leetcode.com/problems/reverse-integer/ Given integer and return this integer with its digits reverse (if reversing integer out of range "int", then return 0) 2. sol if x is minus => abs(x) if x == max, min => return 0 push => x%10 pop => x/10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Solution { public: int reverse(int x) { int ret = 0, xx =x; if(x == INT32_MIN ..
2021.05.11 -
[LeetCode] [Medium] ZigZag Conversion
1. Problem "PAYPALISHIRING" => "PAHNAPLSIIGYIR" Given a string and row of zigzag pattern. return a zigzag pattern string for line by line. 2. Sol1 store in order => each row a vector. and sum all vector 0~numRows-1 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 class Solution { public: string convert(string s, int numRows) { if(numRows == 1) return s; vector answer(numRo..
2021.03.21 -
[LeetCode] [Medium] Longest Palindromic Substring (dp)
1. Problem Given s string and find longest palindromic substring 2. base: len find palindromic based len: s.size() ~ 1 bottom-up: iteration start => 1 -> s.size() You have to do unnecessary nested calculation. and must use dp algorithm. top-down: i teration start => s.size() -> 1 If find palindromic, immediately return substring. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Solution { public:..
2021.03.20 -
[LeetCode] [Medium] Longest Substring Without Repeating Characters
1. Problems Given a string. Find max length of substring without repeating characters. 2. sol I check all ascii for int array. Visited array has index of first finded index. and if find repeating char, Initialize all elements in front of first fineded index. This time complexity is O(N) because inner for statement operate total maximum n. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ..
2021.03.18 -
[LeetCode] [Medium] Add Two Numbers (linked list)
1. Problems Given two num list Add the two numbers and return the sum 2. Sol I use triple pointer for update double pointer. Becuse double pointer have address of answer list and I don't want to change value of answer list. 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 32 33 34 35 36 /** * Definition for singly-linked list. * struct ListNode { * int val; * L..
2021.03.17 -
[LeetCode] [easy] Two Sum (brute-force, map)
1. Problem Given: numbers array and target. Select two element and sum, if output is target return these index. 2. brute-force time-complexity of brute-force is O(N^2) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution { public: vector twoSum(vector& nums, int target) { for(int i = 0; i
2021.03.16