Skip to content

Commit

Permalink
Int To Hex
Browse files Browse the repository at this point in the history
  • Loading branch information
cjlsuper committed Aug 18, 2020
1 parent d7b6525 commit 29a10cf
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions Day20200714.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ int firstUniqChar(char *s);
* */
char *toHex(int num);

// 滑动窗口
char *toHex2(int num);

/**
* 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
* */
Expand All @@ -40,20 +43,49 @@ int firstUniqChar(char *s) {

char *toHex(int num) {

char hash[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
char table[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
char *res = (char *) calloc(9, sizeof(char));
// 最长 8 位!
int idx = 7, cnt = 8;
// 倒置填充索引与计数器
do {
int lowbit4 = num & 0xf;
res[idx--] = table[lowbit4];
num >>= 4;
cnt -= 1;
printf("res = %s\n",res);
printf("low = %c\n",table[lowbit4]);
} while (num != 0 && cnt > 0);
// TODO wtf ???
return res + idx + 1; // 地址截取
}

char *toHex2(int num) {

return hash;
}
char table[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
// 最长 8 位 !
char *res = (char *) calloc(9, sizeof(char));
res[0] = '0';
int move = 28;
int index = 0;
unsigned int highbit4 = 0xf0000000;

void moveZeroes(int *nums, int numsSize) {
while (highbit4 != 0) {
int result = (num & highbit4) >> move;
if (result != 0 || res[0] != '0') {
res[index++] = table[result];
}
move -= 4;
highbit4 >>= 4;
}

return res; // 地址截取
}

void moveZeroes(int *nums, int numsSize) {
for (int i = 0; i < numsSize; ++i) {
printf("%d ", nums[i]);
}


}


Expand All @@ -62,11 +94,12 @@ void test();
void test() {

char *s = "abcabacdef";
printf("firstUniqChar:%d\n", firstUniqChar(s));
// printf("toHex(%d) = %s\n", 5, toHex(5));
// printf("firstUniqChar:%d\n", firstUniqChar(s));

printf("toHex(%p) = %s\n", 255, toHex(255));

int nums[] = {0, 1, 2, 5, 0, 6, 0, 7};
moveZeroes(nums, 8);
// int nums[] = {0, 1, 2, 5, 0, 6, 0, 7};
// moveZeroes(nums, 8);


}
Expand All @@ -76,6 +109,5 @@ int main() {

test();


}

0 comments on commit 29a10cf

Please sign in to comment.