Skip to content

Instantly share code, notes, and snippets.

View stsiwo's full-sized avatar

sts stsiwo

View GitHub Profile
@stsiwo
stsiwo / BaseIdUserType.cs
Last active August 21, 2019 05:51
NHibernate v.5x IUserType Implementation Sample
// Enum (appType) <=> string (dbType)
public class RmqMessageStatusCustomType : IUserType
{
public new bool Equals(object x, object y)
{
if (x == null && y == null)
return true;
if (x == null || y == null)
return false;
@stsiwo
stsiwo / add-two-numbers-linked-list.cpp
Last active January 16, 2021 20:53
LeetCode (level: medium): Add Two Numbers | Linked List
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
@stsiwo
stsiwo / longest-substring-without-repeating-chars.cpp
Created January 16, 2021 21:20
LeetCode (level: medium): Longest Substring Without Repeating Characters
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char, int> map;
int maxLength = 0;
int j = 0;
for (int i = 0; i < s.length(); i++) {
if (map.find(s[i]) != map.end()) {
if (j < map[s[i]] + 1) {
@stsiwo
stsiwo / longest-palindromic-substring.cpp
Created January 19, 2021 22:13
Longest Palindromic Substring
string longestPalindrome(string s) {
int l = 0;
int r = 0;
string result = "";
for (int i = 0; i < s.length(); i++) {
l = r = i;
@stsiwo
stsiwo / valid-parentheses.cpp
Created January 20, 2021 19:01
20. Valid Parentheses
bool isValid(string s) {
if (s.length() < 1 || s.at(0) == ')' || s.at(0) == ']' || s.at(0) == '}') {
return false;
}
// 1. create stack object
stack<char> pStack;
// 2. iterate "s"
for (int i = 0; i < s.length(); i++) {
@stsiwo
stsiwo / merge-two-sorted-lists.cpp
Last active January 21, 2021 17:08
21. Merge Two Sorted Lists
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
@stsiwo
stsiwo / remove-element.cpp
Created January 22, 2021 23:31
27. Remove Element
int removeElement(vector<int>& nums, int val) {
int l = 0;
int r = nums.size() - 1;
while (l <= r) {
// if char at l'th element is equal to val,
// assign char at r'th element to th one at l'th element
if (nums[l] == val) {
nums[l] = nums[r];
@stsiwo
stsiwo / implement-strStr.cpp
Created January 25, 2021 18:56
28. Implement strStr()
/**
* KMP (Knuth Morris Pratt) Pattern Searching
**/
int strStr(string haystack, string needle) {
// return 0 if needle is empty string
if (needle.length() == 0) return 0;
// find sub-patterns in needle
@stsiwo
stsiwo / search-insert-position.cpp
Created January 26, 2021 18:06
35. Search Insert Position
int searchInsert(vector<int>& nums, int target) {
int l = 0;
int r = nums.size() - 1;
// since nums is sorted, we can take an advantage of binary search
while (l <= r) {
int m = (l + r) / 2;
if (nums[m] == target) {
return m;
@stsiwo
stsiwo / Intersection-of-two-linked-lists.cpp
Created January 29, 2021 03:08
160. Intersection of Two Linked Lists
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (!headA || !headB) return nullptr;
ListNode* a = headA;
ListNode* b = headB;
while (a != b) {
a = (a) ? a->next : headB;
b = (b) ? b->next : headA;