diff --git a/binaryWatch/binaryWatch.cpp b/binaryWatch/binaryWatch.cpp new file mode 100644 index 0000000..7ce21ae --- /dev/null +++ b/binaryWatch/binaryWatch.cpp @@ -0,0 +1,109 @@ +// Source : https://leetcode.com/problems/binary-watch/ + +/*************************************************************************************** + * + * A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 + * LEDs on the bottom represent the minutes (0-59). + * Each LED represents a zero or one, with the least significant bit on the right. + * + * For example, the above binary watch reads "3:25". + * + * Given a non-negative integer n which represents the number of LEDs that are + * currently on, return all possible times the watch could represent. + * + * Example: + * Input: n = 1Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", + * "0:16", "0:32"] + * + * Note: + * + * The order of output does not matter. + * The hour must not contain a leading zero, for example "01:00" is not valid, it + * should be "1:00". + * The minute must be consist of two digits and may contain a leading zero, for example + * "10:2" is not valid, it should be "10:02". + ***************************************************************************************/ + +class Solution { +private: + void combination(int nLED, int nLight, int max, bool zero, + int start, int k, int solution, + vector>& result) { + if (solution > max){ + return; + } + if (k == 0) { + char tmp[5] = ""; + if (zero) { + sprintf(tmp, "%02d", solution); + }else{ + sprintf(tmp, "%d", solution); + } + result[nLight].push_back(tmp); + return; + } + for (int i=start; i<=nLED-k; i++) { + solution += pow(2, i); + combination(nLED, nLight, max, zero, i+1, k-1, solution, result); + solution -= pow(2, i); + } + } + + void generate_combination(int nLED, int max, bool zero, vector>& result) { + for (int i=0; i>& vv) { + for(auto v : vv) { + cout << "[ "; + for (auto i : v) { + cout << i << " "; + } + cout << "]" << endl; + } + } + +private: + vector> hour; + vector> mins; + +public: + + Solution():hour(4, vector()), mins(6, vector()){ + generate_combination(4, 11, false, hour); + //print(hour); + //[ 0 ] + //[ 1 2 4 8 ] + //[ 3 5 9 6 10 ] + //[ 7 11 ] + + + generate_combination(6, 59, true, mins); + //print(mins); + //[ 00 ] + //[ 01 02 04 08 16 32 ] + //[ 03 05 09 17 33 06 10 18 34 12 20 36 24 40 48 ] + //[ 07 11 19 35 13 21 37 25 41 49 14 22 38 26 42 50 28 44 52 56 ] + //[ 15 23 39 27 43 51 29 45 53 57 30 46 54 58 ] + //[ 31 47 55 59 ] + } + + vector readBinaryWatch(int num) { + + vector result; + for (int i = 0; i <= 3 && i <= num; i++) { + if (num - i > 5) { + continue; + } + for (auto h : hour[i]) { + for (auto m : mins[num - i]) { + result.push_back( h + ":" + m ); + } + } + + } + return result; + } +}; diff --git a/brokenCalculator/brokenCalculator.cpp b/brokenCalculator/brokenCalculator.cpp new file mode 100644 index 0000000..1f0df5c --- /dev/null +++ b/brokenCalculator/brokenCalculator.cpp @@ -0,0 +1,52 @@ +// Source : https://leetcode.com/problems/broken-calculator/ + +/***************************************************************************************************** + * + * On a broken calculator that has a number showing on its display, we can perform two operations: + * + * Double: Multiply the number on the display by 2, or; + * Decrement: Subtract 1 from the number on the display. + * + * Initially, the calculator is displaying the number X. + * + * Return the minimum number of operations needed to display the number Y. + * + * Example 1: + * + * Input: X = 2, Y = 3 + * Output: 2 + * Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}. + * + * Example 2: + * + * Input: X = 5, Y = 8 + * Output: 2 + * Explanation: Use decrement and then double {5 -> 4 -> 8}. + * + * Example 3: + * + * Input: X = 3, Y = 10 + * Output: 3 + * Explanation: Use double, decrement and double {3 -> 6 -> 5 -> 10}. + * + * Example 4: + * + * Input: X = 1024, Y = 1 + * Output: 1023 + * Explanation: Use decrement operations 1023 times. + * + * Note: + * + * 1 <= X <= 10^9 + * 1 <= Y <= 10^9 + ******************************************************************************************************/ + + +class Solution { +public: + int brokenCalc(int X, int Y) { + if (X >= Y) return X-Y ; + if ( Y%2 ==0 ) return brokenCalc(X, Y/2) + 1; + return brokenCalc(X, Y+1) + 1; + } +}; diff --git a/buddyStrings/buddyStrings.cpp b/buddyStrings/buddyStrings.cpp new file mode 100644 index 0000000..e0bf47a --- /dev/null +++ b/buddyStrings/buddyStrings.cpp @@ -0,0 +1,92 @@ +// Source : https://leetcode.com/problems/buddy-strings/description/ + +/*************************************************************************************** + * + * Given two strings A and B of lowercase letters, return true if and only if we can + * swap two letters in A so that the result equals B. + * + * + * + * Example 1: + * + * + * + * Input: A = "ab", B = "ba" + * Output: true + * + * + * + * Example 2: + * + * + * Input: A = "ab", B = "ab" + * Output: false + * + * + * + * Example 3: + * + * + * Input: A = "aa", B = "aa" + * Output: true + * + * + * + * Example 4: + * + * + * Input: A = "aaaaaaabc", B = "aaaaaaacb" + * Output: true + * + * + * + * Example 5: + * + * + * Input: A = "", B = "aa" + * Output: false + * + * + * + * + * Note: + * + * + * 0 <= A.length <= 20000 + * 0 <= B.length <= 20000 + * A and B consist only of lowercase letters. + * + * + * + * + * + ***************************************************************************************/ + + + +class Solution { +public: + bool buddyStrings(string A, string B) { + if (A.size() != B.size()) return false; + if (A.size()<2) return false; + + bool bRepeat = false; + bool map[26] = {false}; + int idx[2], diffCnt=0; + + for (int i=0; i=2) return false; + idx[diffCnt++] = i; + + } + } + //if A == B and there has repeated chars , then return true + if (diffCnt==0 && bRepeat) return true; + + return (A[idx[0]] == B[idx[1]] && A[idx[1]] == B[idx[0]]); + + } +}; diff --git a/grayCode/GrayCode.cpp b/grayCode/GrayCode.cpp new file mode 100644 index 0000000..5d12346 --- /dev/null +++ b/grayCode/GrayCode.cpp @@ -0,0 +1,150 @@ +// Source : https://oj.leetcode.com/problems/gray-code/ + +/********************************************************************************** +* +* The gray code is a binary numeral system where two successive values differ in only one bit. +* +* Given a non-negative integer n representing the total number of bits in the code, +* print the sequence of gray code. A gray code sequence must begin with 0. +* +* For example, given n = 2, return [0,1,3,2]. Its gray code sequence is: +* +* 00 - 0 +* 01 - 1 +* 11 - 3 +* 10 - 2 +* +* Note: +* For a given n, a gray code sequence is not uniquely defined. +* +* For example, [0,2,3,1] is also a valid gray code sequence according to the above definition. +* +* For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that. +* +**********************************************************************************/ + +#include +#include +#include +#include +#include +using namespace std; + +/* + * I designed the following stupid algorithm base on the blow observation + * + * I noticed I can use a `mirror-like` binary tree to figure out the gray code. + * + * For example: + * + * 0 + * __/ \__ + * 0 1 + * / \ / \ + * 0 1 1 0 + * So, the gray code as below: (top-down, from left to right) + * + * 0 0 0 + * 0 0 1 + * 0 1 1 + * 0 1 0 + * + * 0 + * _____/ \_____ + * 0 1 + * __/ \__ __/ \__ + * 0 1 1 0 + * / \ / \ / \ / \ + * 0 1 1 0 0 1 1 0 + * + * So, the gray code as below: + * + * 0 0 0 0 + * 0 0 0 1 + * 0 0 1 1 + * 0 0 1 0 + * 0 1 1 0 + * 0 1 1 1 + * 0 1 0 1 + * 0 1 0 0 + */ +vector grayCode01(int n) { + vector v; + //n = 1<> 1) ^ num; + * Please refer to http://en.wikipedia.org/wiki/Gray_code + */ +vector grayCode02(int n) { + vector ret; + int size = 1 << n; + for(int i = 0; i < size; ++i) { + ret.push_back((i >> 1)^i); + } + return ret; +} + +//random invoker +vector grayCode(int n) { + srand(time(0)); + if (rand()%2){ + return grayCode01(n); + } + return grayCode02(n); +} + +void printBits(int n, int len){ + for(int i=len-1; i>=0; i--) { + if (n & (1<& v, int bit_len) +{ + vector::iterator it; + + for(it=v.begin(); it!=v.end(); ++it){ + //bitset bin(*it); + printBits(*it, bit_len); + cout << " "; + //cout << *it << " "; + } + cout << endl; +} + +int main(int argc, char** argv) +{ + int n = 2; + if (argc>1){ + n = atoi(argv[1]); + } + vector v = grayCode(n); + printVector(v, n); + return 0; +}