Instantly share code, notes, and snippets.

🎯
Focusing

Jun Zhang zhangxiaomu01

🎯
Focusing
Last active September 2, 2018 02:27
CPP Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 //Brute Force for two sum problems class Solution { public: vector twoSum(vector& nums, int target) { vector Result; int len = nums.size(); for(int i = 0; i< len; i++) { for(int j = i+1; j< len; j++) {
Created September 2, 2018 02:42
Implementation of two sum problem (hash table)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 class Solution { public: vector twoSum(vector& nums, int target) { vector Result; unordered_map hash; for(int i =0 ;i< nums.size(); i++) { int numOfComplement = target - nums[i]; if(hash.find(numOfComplement) != hash.end()) {
Created September 2, 2018 03:02
This is the solution for add two numbers problem in Leetcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public:
Created September 8, 2018 00:02
Longest_Sub_string_without_Repeat_Characters_BruteForce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 class Solution { public: bool isAllUnique(string &s, int start, int end) { unordered_set hash; for(int i = start; i <= end; i++) { if(hash.find(s[i]) != hash.end()) return false; hash.insert(s[i]);
Created September 8, 2018 00:51
Longest_Sub_String_without_Repeat_Characters_Sliding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 class Solution { public: int lengthOfLongestSubstring(string s) { unordered_map set; int n = s.size(); int i=0, j=0, maxLength = 0; while(i
Created September 8, 2018 01:27
Longest_Sub_String_without_Repeat_Characters_Dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 class Solution { public: int lengthOfLongestSubstring(string s) { vector dict(256,-1); int maxLength = 0, start = -1; for(int i =0; i start) start = dict[s[i]]; dict[s[i]] = i;
Last active September 8, 2018 03:13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { int pre = -1, current = -1; int N1 = nums1.size(), N2 = nums2.size(); int aStart = 0, bStart = 0; int total = N1 + N2; for(int i=0; i
Last active September 8, 2018 03:18
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { int N1 = nums1.size(), N2 = nums2.size(); int total = N1 + N2; int left = (total+1)/2; int right = (total + 2) / 2; //If the final array is odd, the median will calculate twice return (getKth(nums1, 0, N1, nums2, 0, N2, left) + getKth(nums1, 0, N1, nums2, 0, N2, right))/2.0; }
Created September 8, 2018 04:22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { int N1 = nums1.size(), N2 = nums2.size(); if(N1 > N2) return findMedianSortedArrays(nums2, nums1); int iMin = 0, iMax = N1; while(iMin <= iMax) {
Created September 14, 2018 23:54
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 class Solution { public: bool isPalindrome(string &s) { int len = s.size(); for(int i = 0; i< len/2; i++) { if(s[i] != s[len - i - 1]) return false; }