Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge #279

Open
wants to merge 293 commits into
base: master
Choose a base branch
from
Open

merge #279

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
293 commits
Select commit Hold shift + click to select a range
5c5363e
fix typo caused build error
xusiwei Feb 25, 2016
d544ca4
Merge pull request #97 from xusiwei/master
haoel Feb 27, 2016
4555282
Merge pull request #95 from wilsoncao/master
haoel Feb 27, 2016
0f33a8d
C solution for "Reverse Words in a String", in O(1) space
xusiwei Feb 27, 2016
be582a0
added increasingTripletSubsequence
Vally79 Feb 27, 2016
2d9734d
Merge pull request #99 from Vally79/master
Vally79 Feb 27, 2016
4c6d32f
Merge pull request #98 from xusiwei/master
haoel Feb 29, 2016
e8e1847
refine the comments
haoel Feb 29, 2016
23801d8
fix #96
haoel Feb 29, 2016
da6bc7c
New Problem "Patching Array"
haoel Feb 29, 2016
3dfaf04
union-find solution for "Surrounded Regions",
xusiwei Mar 12, 2016
38f46e5
added houseRobberIII
Vally79 Apr 29, 2016
968679b
Merge pull request #106 from Vally79/master
Vally79 Apr 29, 2016
d88cc15
added reverseVowelsOfAString
Vally79 Apr 30, 2016
354fa7e
Merge pull request #107 from Vally79/master
Vally79 Apr 30, 2016
a127523
added topKFrequentElements
Vally79 May 2, 2016
62fc7b7
Merge pull request #108 from Vally79/master
Vally79 May 2, 2016
43156ff
added intersectionOfTwoArrays
Vally79 May 20, 2016
f6b0305
Merge pull request #109 from Vally79/master
Vally79 May 20, 2016
7d3ec84
fixed typo
Vally79 May 20, 2016
3dd9e69
Merge pull request #110 from Vally79/master
Vally79 May 20, 2016
96f799f
added intersectionOfTwoArraysII
Vally79 May 22, 2016
c38062d
Merge pull request #111 from Vally79/master
Vally79 May 22, 2016
cf12236
the solution of the problem "Reverse String"
haoel May 29, 2016
32ce962
make the script can detect the author name automatically
haoel May 29, 2016
13eef8d
the solution of problem "Power of Four"
haoel May 29, 2016
35ed8b3
the solution of problem "Integer Break"
haoel May 29, 2016
ecb1789
the solution of the problem "Flatten Nested List Iterator"
haoel May 29, 2016
eb040df
the solution of the problem "Counting Bits"
haoel May 29, 2016
94e9e4b
fix a wrong number in comment
wuxun Jun 14, 2016
0de9d91
Merge pull request #112 from wuxun/master
haoel Jun 21, 2016
b4e0836
Update bullsAndCows.cpp
tvytlx Jul 6, 2016
8168f4b
Merge pull request #116 from arctanx0/master
haoel Aug 7, 2016
227b574
added combinationSumIV
Vally79 Aug 7, 2016
0872617
Merge pull request #117 from Vally79/master
Vally79 Aug 7, 2016
931ba6a
added wiggleSubsequence
Vally79 Aug 8, 2016
ec9b5ac
Merge pull request #118 from Vally79/master
Vally79 Aug 8, 2016
ec8013d
modified: cpp/intersectionOfTwoArrays/intersectionOfTwoArrays.cpp
Aug 11, 2016
0c322fe
Merge pull request #119 from FEIGONG/master
haoel Aug 23, 2016
5e14762
New Problem "Lexicographical Numbers"
haoel Aug 23, 2016
5fe30bc
New Problem "First Unique Character in a String"
haoel Aug 23, 2016
25633c1
add indent
haoel Aug 23, 2016
f9dc634
New Problem "Longest Absolute File Path"
haoel Aug 23, 2016
bfeb664
typo
haoel Aug 23, 2016
729eec6
uncomment the code
haoel Aug 24, 2016
d076c2c
New Problem Solution - "Mini Parser"
haoel Aug 24, 2016
d666cbf
New Problem Solution - "Shuffle an Array"
haoel Aug 24, 2016
6906cda
New Problem Solution - "Ransom Note"
haoel Aug 24, 2016
9def962
New Problem Solution - "Linked List Random Node"
haoel Aug 24, 2016
2c9f3c2
Two Problems Solutions - "Insert Delete GetRandom O(1)"
haoel Aug 25, 2016
4eb4806
modified: algorithms/cpp/maximumDepthOfBinaryTree/maximumDepthOfBi…
Aug 31, 2016
7f16adf
add readme.sh script to help to generate the content in README.md for…
haoel Sep 7, 2016
1714519
New Problem Solution - "Find the Difference"
haoel Sep 7, 2016
a4b191a
New Problem Solution "Elimination Game"
haoel Sep 7, 2016
626efee
New Problem Solution "Perfect Rectangle"
haoel Sep 7, 2016
ea1a213
New Problem "Is Subsequence"
haoel Sep 7, 2016
cfe8809
New Problem Solution "UTF-8 Validation"
haoel Sep 7, 2016
2f3a77d
New Problem Solution"Longest Substring with At Least K Repeating Char…
haoel Sep 7, 2016
032602d
Merge pull request #122 from FEIGONG/master
haoel Oct 10, 2016
5353d9e
Merge pull request #101 from xusiwei/master
haoel Oct 10, 2016
2d433dd
correct the URL of "Elimination Game"
haoel Nov 2, 2016
421189e
New Problem Solution "Rotate Function"
haoel Nov 2, 2016
6becbec
New Problem Solution "Integer Replacement"
haoel Nov 4, 2016
51a0624
New Problem Solution "Random Pick Index"
haoel Nov 4, 2016
f9e16a7
New Problem Solution "Evaluate Division"
haoel Nov 4, 2016
fa4d3f6
New Problem Solution "Nth Digit"
haoel Nov 5, 2016
4285701
New Problem Solution "Binary Watch"
haoel Nov 5, 2016
fff0041
make the variables name more sense
haoel Nov 5, 2016
a991e74
New Problem Solution - "Remove K Digits"
haoel Nov 11, 2016
24839ce
New Problem Solution "Sum of Left Leaves"
haoel Nov 11, 2016
51e3e7f
fix: change the direcotry
haoel Nov 11, 2016
7961704
New Problem Solution "Convert a Number to Hexadecimal"
haoel Nov 11, 2016
0e3f0e2
New Problem Solution "Frog Jump"
haoel Nov 12, 2016
7fb99ce
New Problem Solution "Queue Reconstruction by Height"
haoel Nov 12, 2016
9f00ad1
New Problem Solution "Add Strings"
haoel Nov 12, 2016
c54086b
adjust the file's directory
haoel Nov 12, 2016
e7e1252
New Problem "Arithmetic Slices"
haoel Nov 13, 2016
48978d8
New Problem Solution "Fizz Buzz"
haoel Nov 13, 2016
4d68a13
New Problem Solution "Longest Palindrome"
haoel Nov 13, 2016
0941073
New Problem Soluiton "Split Array Largest Sum"
haoel Nov 13, 2016
f9a8a8b
New Problem Solution "Sentence Screen Fitting"
yiakwy Nov 28, 2016
a3dd93d
add heart
yiakwy Nov 28, 2016
8d833c1
New method (non recursive) for CourseSchedule
yiakwy Nov 28, 2016
879e9e6
array index out of bounds, fix to low<n-1
omengye Dec 26, 2016
b0507b1
Merge pull request #128 from omengye/patch-1
haoel Jan 2, 2017
1a3bf58
New Problem Solution - "Super Ugly Number"
haoel Jan 2, 2017
aed69e7
fix: forgot commit the "Find the Difference"
haoel Jan 2, 2017
9c3eac7
New Problem Solution "Remove Duplicate Letters"
haoel Jan 2, 2017
f3c62b3
New Problem Solution - "Maximum Product of Word Lengths"
haoel Jan 2, 2017
7fc247a
New Problem Solution "Wiggle Sort II"
haoel Jan 2, 2017
86f9497
New Problem Solution "Verify Preorder Serialization of a Binary Tree"
haoel Jan 6, 2017
121b458
New Problem Solution "Reconstruct Itinerary"
haoel Jan 6, 2017
271fb95
added totalHammingDistance
Vally79 Jan 9, 2017
2ee4f1f
Merge pull request #129 from Vally79/master
Vally79 Jan 9, 2017
1625e4a
fix index out of range
se77en Jan 18, 2017
a99649d
Merge pull request #130 from se77en/patch-1
haoel Mar 15, 2017
fe04640
New solution for "Reverse Vowels of a String"
haoel Mar 21, 2017
ad9d290
New Problem Solution "Palindrome Pairs"
haoel Mar 22, 2017
0cf4a89
add drive ai questions P1
yiakwy Apr 12, 2017
1567d0e
add new problem
yiakwy Apr 12, 2017
007e5a1
New Problem Solution "Sentence Screen Fitting"
yiakwy Nov 28, 2016
0398a78
add heart
yiakwy Nov 28, 2016
27c10d3
New method (non recursive) for CourseSchedule
yiakwy Nov 28, 2016
ace44a5
add drive ai questions P1
yiakwy Apr 12, 2017
8c947bc
add new problem
yiakwy Apr 12, 2017
b522011
fix
yiakwy Apr 13, 2017
109c6d6
杨氏矩阵的查找
kosoraYintai Jan 27, 2018
70f6a59
Test for problem 240
kosoraYintai Jan 27, 2018
1a6c424
Comments for problem 240
kosoraYintai Jan 27, 2018
fb1dc6e
杨氏矩阵的查找
kosoraYintai Jan 27, 2018
59eee6f
Test for problem 240
kosoraYintai Jan 27, 2018
eee437f
Comments for problem 240
kosoraYintai Jan 27, 2018
f037a8e
read me format
Sep 18, 2017
b4b73ec
handle cases with input array size < 3
karanchawla Oct 8, 2017
92562b3
added database support
Jun 5, 2017
f7c374f
Merge pull request #1 from haoel/master
kosoraYintai Jan 31, 2018
ca71b67
Leetcode changed the frontend to GraphQL API, the scripts need to ada…
haoel Feb 1, 2018
2338d1f
move the xidel installation script from comments.sh to lib/query_prob…
haoel Feb 2, 2018
dffcf2e
typo fix, close #120
haoel Feb 2, 2018
47a220b
Merge branch 'master' of https://github.com/yiakwy/leetcode into yiak…
haoel Feb 2, 2018
b838e2f
Merge branch 'yiakwy-master'
haoel Feb 2, 2018
551993a
Create firstMd.md
kosoraYintai Feb 2, 2018
4712378
Update firstMd.md
kosoraYintai Feb 2, 2018
b5729f6
Update firstMd.md
kosoraYintai Feb 2, 2018
2b133ce
deleteMd haha
kosoraYintai Feb 2, 2018
63341a8
Merge pull request #2 from haoel/master
kosoraYintai Feb 2, 2018
1b630ca
Some array、stack、queue solution for leetcode
kosoraYintai Feb 2, 2018
cf79335
Readme.md for problem 26, problem 155, problem 189, problem 225 and p…
kosoraYintai Feb 2, 2018
7b83329
rename dir/file
kosoraYintai Feb 3, 2018
1da21ef
rename dir/file
kosoraYintai Feb 3, 2018
f31071e
rename dir/file
kosoraYintai Feb 3, 2018
ec2c5f7
Merge branch 'kosoraYintai-master'
haoel Feb 4, 2018
eb6b031
remove `DockerCircles` and add problem description for sentence seree…
haoel Feb 4, 2018
9e4c01b
fix the bug
andyli029 Aug 12, 2017
cfd9293
[Linux]fix the download link - xidel download link seems use differen…
haoel Feb 5, 2018
0fdf100
fix: the xidel rename the -q option to -s after 0.9.4 version, so we …
haoel Feb 5, 2018
726e762
Java Solution for 9、38、50、58、92、125、151、206、242, KMP for problem 9
kosoraYintai Feb 11, 2018
725a380
Java Solution for 9、38、50、58、92、125、151、206、242, KMP for problem 9
kosoraYintai Feb 11, 2018
cd38a47
Two New Problem Solution - "Most Common Word" and "Maximum Average Su…
haoel Apr 19, 2018
5dea314
fix the comments.sh which use the new leetcode API to retrive problem…
haoel Apr 19, 2018
4d48134
[fix] - adopted the new leetcode GrapQL API
haoel Jun 23, 2018
b98dab8
[New Solution] - "Jewels and Stones"
haoel Jun 23, 2018
956807c
[fix] fix the bugs of DP
haoel Jun 23, 2018
dcaa897
[fix] remove the debug loging
haoel Jun 24, 2018
a3483c9
New Solution "Partition Equal Subset Sum"
haoel Jun 25, 2018
7228f4e
Updated an simple solution for two easy problems
haoel Jun 25, 2018
034fee5
New Solution - "Sum of Two Integers"
haoel Jun 25, 2018
e364610
New Solution "Valid Perfect Square"
haoel Jun 25, 2018
e66a31f
fix: using `questionFrontendId` instead of `questionId`
haoel Jun 27, 2018
93d6399
New Solution "Mirror Reflection"
haoel Jun 27, 2018
fdf5f82
New Solution "Buddy Strings"
haoel Jun 27, 2018
42ec491
New Solution "Unique Morse Code Words"
haoel Jun 29, 2018
3076f88
New Solution"Peak Index in a Mountain Array"
haoel Jun 29, 2018
9de0296
New Solution "Backspace String Compare"
haoel Jun 29, 2018
7df02ee
New Solution "Integer Replacement"
ehds Aug 1, 2018
557834f
add python solution to some questions
Kurt-Yu Dec 22, 2018
00af522
added python solution to some problems
Kurt-Yu Dec 26, 2018
f01883e
added python solutions to some problems
Kurt-Yu Dec 31, 2018
c08e32f
added python solutions to some problems
Kurt-Yu Jan 2, 2019
f1e4d92
added python solutions to some problems
Kurt-Yu Jan 9, 2019
01320b2
added some python solutions
Kurt-Yu Jan 14, 2019
f380f20
added some python solutions
Kurt-Yu Jan 21, 2019
b9196c3
added some python solutions
Kurt-Yu Jan 23, 2019
c28ac08
added some python solutions
Kurt-Yu Jan 25, 2019
ed86932
fixed a small markdown syntax
Kurt-Yu Jan 25, 2019
046bb1c
fix the graphQL API request payload body
haoel Jan 26, 2019
00482f1
fix the bug with question title
haoel Jan 26, 2019
f54ad38
optimized the scripts to generate the comments
haoel Jan 26, 2019
d73a5a0
New Solution "Longest Turbulent Subarray"
haoel Jan 27, 2019
35ecea3
New Problem Solution "String Without AAA or BBB"
haoel Jan 29, 2019
bcb337f
New Problem Solution "Minimum Cost For Tickets"
haoel Jan 29, 2019
d675cfa
missed coommit the source files
haoel Jan 29, 2019
50a6de1
New Problem Solution "Time Based Key-Value Store"
haoel Jan 30, 2019
ef13f1e
New Problem Solution "Palindromic Substrings"
haoel Jan 30, 2019
e1012ef
New Problem Solution "Minimum Falling Path Sum"
haoel Jan 30, 2019
b0daa57
New Problem Solution "Minimum ASCII Delete Sum for Two String"
haoel Jan 30, 2019
0f15689
fix #165 - correct the difficulty level
haoel Jan 30, 2019
6fcccec
correct the link from "https://oj.leetcode.com/" to "https://leetcode…
haoel Jan 30, 2019
59204d7
fix #160 - when the input is "-2147483648 -1",the array named bit_num…
haoel Jan 30, 2019
56f72e6
optimzing the logic - reduced the control state
haoel Jan 31, 2019
b6fe67c
optimzing the code
haoel Jan 31, 2019
0b8e7d9
New Problem Solution "Best Time to Buy and Sell Stock with Transactio…
haoel Feb 1, 2019
7249bc3
New Problem Solution "Best Time to Buy and Sell Stock with Cooldown"
haoel Feb 1, 2019
4d762f4
add a new simpler solution
haoel Feb 2, 2019
d07df71
New Problem Solution "Unique Paths III"
haoel Feb 4, 2019
4b2f4d0
optimzating the implementation of Unique Paths I/II
haoel Feb 4, 2019
9b23fc2
New Problem C++ Solution - "Min Cost Climbing Stairs"
haoel Feb 4, 2019
0cda1f9
New Problem Solution "Detect Capital"
haoel Feb 4, 2019
fe4dd65
New Problem Solution "Island Perimeter"
haoel Feb 4, 2019
0f0c10c
New Problem Solution - "Sum of Even Numbers After Queries"
haoel Feb 5, 2019
b585d36
New Problem Solution - "Smallest String Starting From Leaf"
haoel Feb 5, 2019
b5e252b
New Problem Solution - "Vertical Order Traversal of a Binary Tree"
haoel Feb 5, 2019
fed3639
New Probelm Solution = "Interval List Intersections"
haoel Feb 5, 2019
3b49afb
added a recursive solution
haoel Mar 18, 2019
0674c9b
New Problem Solution "Coin Change 2"
haoel Mar 18, 2019
1965803
fix uniquePaths.II solution
ehds Feb 26, 2019
a8bdcb8
fix uniquePaths.II cpp solution
ehds Feb 26, 2019
5f94d17
fix uniquePaths.II cpp solution
ehds Feb 26, 2019
8bc818a
fix #167 : need to check `y` is overflowed
haoel Mar 18, 2019
054e426
New Problem Solution - "Game of Life"
haoel Mar 20, 2019
1b0bf37
add Hao's implementation
haoel Mar 21, 2019
4905e7f
New Problem Solution "Count Numbers with Unique Digits"
haoel Mar 24, 2019
5353720
House Robber III - another implementation
haoel Mar 24, 2019
395a4d8
reformat the script
haoel Mar 25, 2019
86183f9
fix the bug - tr: Illegal byte sequence
haoel Mar 25, 2019
f18b025
New Problem Solution "Add to Array-Form of Integer"
haoel Mar 25, 2019
2f01d89
New Problem Solution "Satisfiability of Equality Equations"
haoel Mar 25, 2019
646a155
fix: funciton name
haoel Mar 26, 2019
2ed6107
New Problem C++ Solution - "Squares of a Sorted Array"
haoel Mar 26, 2019
30fbc75
New Problem Solution - "Sort Array By Parity"
haoel Mar 26, 2019
e381e32
New Problem Solution - "Sort Array By Parity II"
haoel Mar 26, 2019
7d61861
New Problem C++ Solution - "Fibonacci Number"
haoel Mar 26, 2019
97ca258
New Problem Solution - "Max Area of Island"
haoel Mar 26, 2019
d099f61
slightly reformat the code
haoel Mar 26, 2019
ab23bb7
New Problem Solution - "Friend Circles"
haoel Mar 26, 2019
f5859be
New Problem Solution - "Accounts Merge"
haoel Mar 28, 2019
c20038f
New Problem Solution - "Distribute Coins in Binary Tree"
haoel Mar 29, 2019
db99fe1
no tricky solution
haoel Mar 29, 2019
2017083
refactory the code to make it a bit readable
haoel Mar 30, 2019
5db7fa4
more solutions
haoel Mar 30, 2019
4bffdab
more readable implementation
haoel Mar 30, 2019
1ebef5c
slight midification and removed the debug code
haoel Mar 31, 2019
56720e9
simplified the implmentation
haoel Mar 31, 2019
4c98ae0
fixed some algorithm bugs
haoel Apr 1, 2019
8a82252
fixed the algorithm bug
haoel Apr 2, 2019
d0b2896
update the comments
haoel Apr 2, 2019
d3946ff
more clean implementation
haoel Apr 2, 2019
8e851c8
update the implmetation
haoel Apr 2, 2019
c4e9249
update the problem description
haoel Apr 3, 2019
8e8d43d
refactory the code : show the evolution of the solution
haoel Apr 3, 2019
6d30ea6
reformat the comments
haoel Apr 3, 2019
ff4280d
more clean implementation
haoel Apr 3, 2019
3866064
update the comments
haoel Apr 8, 2019
aad9846
add n TLE solution
haoel Apr 8, 2019
ba6a84d
New Problem Solution - "Two City Scheduling"
haoel Apr 21, 2019
6c9f558
New Problem Solution - "Matrix Cells in Distance Order"
haoel Apr 21, 2019
467fd31
New Problem Solution "Cousins in Binary Tree"
haoel Apr 30, 2019
bb0ede1
New Problem Solution "Broken Calculator"
haoel Apr 30, 2019
3564a92
New C++ Solution "Largest Perimeter Triangle"
yue2388253 Apr 7, 2019
f15b164
New C++ Solution "Largest Perimeter Triangle"
yue2388253 Apr 7, 2019
406871b
Update IntegerBreak.cpp
LynnTh May 14, 2019
eab7816
recoverTreeFromPreorderTraversal.cpp
AhmedMorsy95 May 19, 2019
1ddea28
refine the file name and the problem description comment #172
haoel May 29, 2019
1f3f2db
addTwoNumbers golang solution
May 29, 2019
9f09f91
modify name to twoSum and readme
May 29, 2019
7191254
add ,
May 29, 2019
27c5c9e
modify golang file name
May 29, 2019
18d72f2
add an log alias in git config file
haoel May 29, 2019
1314a81
New Problem Solution "Compare Strings by Frequency of the Smallest Ch…
haoel Oct 1, 2019
bde5dff
New Problem Solution "Video Stitching"
haoel Oct 1, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[alias]
lg0 = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --
lg1 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
lg = !"git lg0"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
algorithms-java/out
*.class
551 changes: 350 additions & 201 deletions README.md

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions algorithms/cpp/3Sum/3Sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using namespace std;


/*
* Simlar like "Two Number" problem, we can have the simlar solution.
* Similar like "Two Number" problem, we can have the simlar solution.
*
* Suppose the input array is S[0..n-1], 3SUM can be solved in O(n^2) time on average by
* inserting each number S[i] into a hash table, and then for each index i and j,
Expand All @@ -47,6 +47,7 @@ using namespace std;
vector<vector<int> > threeSum(vector<int> &num) {

vector< vector<int> > result;
if(num.size()==0 || num.size()==1 || num.size() == 2) return result;

//sort the array, this is the key
sort(num.begin(), num.end());
Expand All @@ -71,7 +72,7 @@ vector<vector<int> > threeSum(vector<int> &num) {
result.push_back(v);
// Continue search for all triplet combinations summing to zero.
//skip the duplication
while(low<n && num[low]==num[low+1]) low++;
while(low<n-1 && num[low]==num[low+1]) low++;
while(high>0 && num[high]==num[high-1]) high--;
low++;
high--;
Expand All @@ -81,7 +82,7 @@ vector<vector<int> > threeSum(vector<int> &num) {
high--;
} else{
//skip the duplication
while(low<n && num[low]==num[low+1]) low++;
while(low<n-1 && num[low]==num[low+1]) low++;
low++;
}
}
Expand Down
86 changes: 86 additions & 0 deletions algorithms/cpp/UTF8Validation/UTF8Validation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Source : https://leetcode.com/problems/utf-8-validation/
// Author : Hao Chen
// Date : 2016-09-08

/***************************************************************************************
*
* A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:
*
* For 1-byte character, the first bit is a 0, followed by its unicode code.
* For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by
* n-1 bytes with most significant 2 bits being 10.
*
* This is how the UTF-8 encoding would work:
*
* Char. number range | UTF-8 octet sequence
* --------------------+---------------------------------------------
* 0000 0000-0000 007F | 0xxxxxxx
* 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
* 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
* 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
*
* Given an array of integers representing the data, return whether it is a valid utf-8
* encoding.
*
* Note:
* The input is an array of integers. Only the least significant 8 bits of each integer
* is used to store the data. This means each integer represents only 1 byte of data.
*
* Example 1:
*
* data = [197, 130, 1], which represents the octet sequence: 11000101 10000010
* 00000001.
*
* Return true.
* It is a valid utf-8 encoding for a 2-bytes character followed by a 1-byte character.
*
* Example 2:
*
* data = [235, 140, 4], which represented the octet sequence: 11101011 10001100
* 00000100.
*
* Return false.
* The first 3 bits are all one's and the 4th bit is 0 means it is a 3-bytes character.
* The next byte is a continuation byte which starts with 10 and that's correct.
* But the second continuation byte does not start with 10, so it is invalid.
***************************************************************************************/


class Solution {
public:
bool validUtf8(vector<int>& data) {
int i = 0;
while ( i < data.size() ) {
if ( (data[i] & 0x80) == 0 ){
i++;
continue;
}

int len = 0;
if ( (data[i] & 0xE0) == 0xC0 ) { // checking 110xxxxx
len = 2;
}else if ( (data[i] & 0xF0) == 0xE0) { // checking 1110xxxx
len = 3;
}else if ( (data[i] & 0xF8) == 0xF0) { // checking 11110xxx
len = 4;
}else {
return false;
}


for (int j=i+1; j < i+len; j++) { //checking 10xxxxxx
if ( (data[j] & 0xC0) != 0x80 ) {
return false;
}
}

i += len ;

if (i > data.size()) {
return false;
}

}
return true;
}
};
174 changes: 174 additions & 0 deletions algorithms/cpp/accountsMerge/AccountsMerge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// Source : https://leetcode.com/problems/accounts-merge/
// Author : Hao Chen
// Date : 2019-03-29

/*****************************************************************************************************
*
* Given a list accounts, each element accounts[i] is a list of strings, where the first element
* accounts[i][0] is a name, and the rest of the elements are emails representing emails of the
* account.
*
* Now, we would like to merge these accounts. Two accounts definitely belong to the same person if
* there is some email that is common to both accounts. Note that even if two accounts have the same
* name, they may belong to different people as people could have the same name. A person can have
* any number of accounts initially, but all of their accounts definitely have the same name.
*
* After merging the accounts, return the accounts in the following format: the first element of each
* account is the name, and the rest of the elements are emails in sorted order. The accounts
* themselves can be returned in any order.
*
* Example 1:
*
* Input:
* accounts = [["John", "[email protected]", "[email protected]"], ["John", "[email protected]"],
* ["John", "[email protected]", "[email protected]"], ["Mary", "[email protected]"]]
* Output: [["John", '[email protected]', '[email protected]', '[email protected]'], ["John",
* "[email protected]"], ["Mary", "[email protected]"]]
*
* Explanation:
* The first and third John's are the same person as they have the common email "[email protected]".
* The second John and Mary are different people as none of their email addresses are used by other
* accounts.
*
* We could return these lists in any order, for example the answer [['Mary', '[email protected]'],
* ['John', '[email protected]'],
* ['John', '[email protected]', '[email protected]', '[email protected]']] would still be accepted.
*
* Note:
* The length of accounts will be in the range [1, 1000].
* The length of accounts[i] will be in the range [1, 10].
* The length of accounts[i][j] will be in the range [1, 30].
******************************************************************************************************/


//Bad Performance Solution
class Solution_Time_Limit_Exceeded {
public:
// We can orginze all relevant emails to a chain,
// then we can use Union Find algorithm
// Besides, we also need to map the relationship between name and email.
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
unordered_map<string, string> emails_chains; // email chains
unordered_map<string, string> names; // names to email chains' head

//initialization
for(int i = 0 ; i<accounts.size();i++) {
auto& account = accounts[i];
auto& name = account[0];
for (int j=1; j<account.size(); j++) {
auto& email = account[j];
if ( names.find(email) == names.end() ) {
emails_chains[email] = email;
names[email] = name;
}
join(emails_chains, account[1], email);
}
}

//reform the emails
unordered_map<string, set<string>> res;
for( auto& acc : accounts ) {
string e = find(emails_chains, acc[1]);
res[e].insert(acc.begin()+1, acc.end());
}

//output the result
vector<vector<string>> result;
for (auto pair : res) {
vector<string> emails(pair.second.begin(), pair.second.end());
emails.insert(emails.begin(), names[pair.first]);
result.push_back(emails);
}
return result;
}

string find(unordered_map<string, string>& emails_chains,
string email) {
while( email != emails_chains[email] ){
email = emails_chains[email];
}
return email;
}

bool join(unordered_map<string, string>& emails_chains,
string& email1, string& email2) {
string e1 = find(emails_chains, email1);
string e2 = find(emails_chains, email2);
if ( e1 != e2 ) emails_chains[e1] = email2;
return e1 == e2;
}
};

//
// Performance Tunning
// -----------------
//
// The above algorithm need to do string comparison, it causes lots of efforts
// So, we allocated the ID for each email, and compare the ID would save the time.
//
// Furthermore, we can use the Group-Email-ID instead of email ID,
// this would save more time.
//
class Solution {
public:
// We can orginze all relevant emails to a chain,
// then we can use Union Find algorithm
// Besides, we also need to map the relationship between name and email.
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
unordered_map<string, int> emails_id; //using email ID for union find
unordered_map<int, int> emails_chains; // email chains
unordered_map<int, string> names; // email id & name

//initialization & join
for(int i = 0 ; i<accounts.size();i++) {

// using the account index as the emails group ID,
// this could simplify the emails chain.
emails_chains[i] = i;

auto& account = accounts[i];
auto& name = account[0];
for (int j=1; j<account.size(); j++) {
auto& email = account[j];
if ( emails_id.find(email) == emails_id.end() ) {
emails_id[email] = i;
names[i] = name;
}else {
join( emails_chains, i, emails_id[email] );
}

}
}

//reform the emails
unordered_map<int, set<string>> res;
for(int i=0; i<accounts.size(); i++) {
int idx = find(emails_chains, i);
res[idx].insert(accounts[i].begin()+1, accounts[i].end());
}


//output the result
vector<vector<string>> result;
for (auto pair : res) {
vector<string> emails( pair.second.begin(), pair.second.end() );
emails.insert(emails.begin(), names[pair.first]);
result.push_back(emails);
}
return result;
}

int find(unordered_map<int, int>& emails_chains, int id) {
while( id != emails_chains[id] ){
id = emails_chains[id];
}
return id;
}

bool join(unordered_map<int, int>& emails_chains, int id1, int id2) {
int e1 = find(emails_chains, id1);
int e2 = find(emails_chains, id2);
if ( e1 != e2 ) emails_chains[e1] = e2;
return e1 == e2;
}
};
48 changes: 48 additions & 0 deletions algorithms/cpp/addStrings/AddStrings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Source : https://leetcode.com/problems/add-strings/
// Author : Hao Chen
// Date : 2016-11-12

/***************************************************************************************
*
* Given two non-negative numbers num1 and num2 represented as string, return the sum
* of num1 and num2.
*
* Note:
*
* The length of both num1 and num2 is
* Both num1 and num2 contains only digits 0-9.
* Both num1 and num2 does not contain any leading zero.
* You must not use any built-in BigInteger library or convert the inputs to integer
* directly.
***************************************************************************************/

class Solution {
public:
string addStrings(string num1, string num2) {
string& longstr = ( num1.size() >= num2.size() ? num1 : num2 );
string& shortstr = ( num1.size() < num2.size() ? num1 : num2 );

int longlen = longstr.size();
int shortlen = shortstr.size();

char carry = 0;
int i, j;

string result;
for (i = longlen-1, j=shortlen-1; i>=0; i--, j--) {
int add = 0;
if (j>=0) {
add = longstr[i] + shortstr[j] - 2 * '0' + carry;
}else{
add = longstr[i] - '0' + carry;
}
carry = add/10;
result = char('0' + add % 10) + result;
}

if (carry) {
result = '1' + result;
}
return result;
}
};
Loading