Skip to content

Instantly share code, notes, and snippets.

View zhangxiaomu01's full-sized avatar
🎯
Focusing

Jun Zhang zhangxiaomu01

🎯
Focusing
View GitHub Profile
//Brute Force for two sum problems
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> Result;
int len = nums.size();
for(int i = 0; i< len; i++)
{
for(int j = i+1; j< len; j++)
{
@zhangxiaomu01
zhangxiaomu01 / Two_Sum_Hash.cpp
Created September 2, 2018 02:42
Implementation of two sum problem (hash table)
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> Result;
unordered_map<int, int> hash;
for(int i =0 ;i< nums.size(); i++)
{
int numOfComplement = target - nums[i];
if(hash.find(numOfComplement) != hash.end())
{
@zhangxiaomu01
zhangxiaomu01 / Add_Two_Numbers.cpp
Created September 2, 2018 03:02
This is the solution for add two numbers problem in Leetcode
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
@zhangxiaomu01
zhangxiaomu01 / Longest_Sub_string_without_Repeat_Characters_BruteForce.cpp
Created September 8, 2018 00:02
Longest_Sub_string_without_Repeat_Characters_BruteForce
class Solution {
public:
bool isAllUnique(string &s, int start, int end)
{
unordered_set<char> hash;
for(int i = start; i <= end; i++)
{
if(hash.find(s[i]) != hash.end())
return false;
hash.insert(s[i]);
@zhangxiaomu01
zhangxiaomu01 / Longest_Sub_String_without_Repeat_Characters_Sliding.cpp
Created September 8, 2018 00:51
Longest_Sub_String_without_Repeat_Characters_Sliding
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char, int> set;
int n = s.size();
int i=0, j=0, maxLength = 0;
while(i<n&&j<n)
{
if(set.find(s[j])!=set.end())
{
@zhangxiaomu01
zhangxiaomu01 / Longest_Sub_String_without_Repeat_Characters_Dictionary.cpp
Created September 8, 2018 01:27
Longest_Sub_String_without_Repeat_Characters_Dictionary
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> dict(256,-1);
int maxLength = 0, start = -1;
for(int i =0; i<s.size(); i++)
{
if(dict[s[i]] > start)
start = dict[s[i]];
dict[s[i]] = i;
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& 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<total/2 + 1; i++)
{
pre = current;
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& 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;
}
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int N1 = nums1.size(), N2 = nums2.size();
if(N1 > N2)
return findMedianSortedArrays(nums2, nums1);
int iMin = 0, iMax = N1;
while(iMin <= iMax)
{
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;
}