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

找茬 #13

Open
lzl124631x opened this issue Mar 3, 2014 · 5 comments
Open

找茬 #13

lzl124631x opened this issue Mar 3, 2014 · 5 comments

Comments

@lzl124631x
Copy link

92页, 归并排序
出现了连续两行
/* 数组元素的类型/
typedef int elem_t;

merge函数中第一个循环 for(i = 0...) 应该是 for(i = start...)

基数排序, 应该提供给用户统一的接口, 即void Sort(int A[], int len)
96页中的 a[0].link = current = front[j]; 应改为 a[0].link = front[j];
getDigit中的10应该改为R

第四页strstr第一个for循环的继续条件应该是 _p1_advance && *p 否则会访问越界. 此外, 第二个for里的while不必判断_p1因为p1不会越过p1_advance.
我原先的Leet Code代码

class Solution {
public:
    char *strStr(char *haystack, char *needle) {
        while(true){
            char *p = haystack, *q = needle;
            while(*p && *q && *p == *q){
                p++; q++;
            }
            if(!*q){
                return haystack;
            }else if(*p){
                haystack++;
            }else{
                return NULL;
            }
        }
    }
};

看了你的代码后借鉴着写了:

class Solution {
public:
    char *strStr(char *haystack, char *needle) {
        if(!haystack || !needle) return NULL;
        if(!*needle) return haystack;
        char *p = haystack, *pEnd = haystack, *q = needle + 1;
        while(*pEnd && *q){ pEnd++; q++; }
        while(*pEnd){
            p = haystack, q = needle;
            while(*q && *p == *q){ p++; q++; }
            if(!*q) return haystack;
            haystack++;
            pEnd++;
        }
        return NULL;
    }
};

顺便问一下, 有什么快捷方法把我贴上去的代码直接转成"代码样式"嘛? 我是每行行首手动复制了四个空格...有些麻烦

@lushl9301
Copy link

'''顺便问一下, 有什么快捷方法把我贴上去的代码直接转成"代码样式"嘛? 我是每行行首手动复制了四个空格...有些麻烦'''

有些ide(比如eclipse) 支持一键自动ident。全选之后 ctrl + shift + F 就会自动把缩进调整好。
(虽然还是有点点麻烦……XD)

@lzl124631x
Copy link
Author

希尔排序的shell_insert里面:
for (j = i - gap; tmp < a[j] && j >= start; j -= gap) {
这里应该先判断 j >= start 再 访问a[j].
另外感觉你的shell_insert不应该是从 i = start+gapi < end, 那样会导致很多元素重复进行判断插入. 下面是我的代码, i是每次插入排序的起始点, j是每次插入的当前点, k是每次插入向前扫描的指针.

void shellInsertionSort(int A[], int len){
    if(!A || len < 2) return;
    int gap = len;
    while(gap > 1){
        gap = gap / 3 + 1;
        for(int i = 0; i < gap; ++i){
            for(int j = i + gap; j < len; j += gap){
                int k = j, tmp = A[j];
                for(; k > i && A[k - gap] > tmp; k -= gap){
                    A[k] = A[k - gap];
                }
                A[k] = tmp;
            }
        }
    }
}

还有, 我问的不是IDE中如何自动indent, 而是这GitHub中如何自动让代码变成上面这种框框. 看了一下这里https://help.github.com/articles/github-flavored-markdown, 原来用 ``` 包裹起来就好了.

@soulmachine
Copy link
Owner

感谢找茬,要多多找茬,我有时间会仔细看一下:)

@yinwuzhe
Copy link

yinwuzhe commented Aug 8, 2014

作者你好,我刚刚看这本书,对于第一章说的一点有点怀疑
“判断一个整数是否是为奇数,用 x % 2 != 0,不要用 x % 2 == 1,因为 x 可能是
负数。”

但是我经过实验发现即使是复数,使用 x % 2 == 1也可以负数的奇数可以找出来。。

实验平台:x86-64
代码:
#include <stdio.h>

int main()
{
int num;
scanf("%d",&num);
if((num%2)==1)
printf("%d is odd number\n", num);
return 0;
}

运行结果:
[root@localhost soulmachine]# ./cha1
-9
-9 is odd number

gcc版本号:
gcc version 4.7.2 20121109 (Red Hat 4.7.2-8) (GCC)

@soulmachine
Copy link
Owner

这个技巧是跟编译器相关的,因为C++标准没有规定负数除法,所以不同的编译器是不同的

现代的gcc , vc的等编译器尽可能与主流保持一致,所以我的这个技巧已经过时了

On Friday, August 8, 2014, yinwuzhe [email protected] wrote:

作者你好,我刚刚看这本书,对于第一章说的一点有点怀疑
“判断一个整数是否是为奇数,用 x % 2 != 0,不要用 x % 2 == 1,因为 x 可能是
负数。”

但是我经过实验发现即使是复数,使用 x % 2 == 1也可以负数的奇数可以找出来。。

实验平台:x86-64
代码:
#include

int main()
{
int num;
scanf("%d",&num);
if((num%2)==1)
printf("%d is odd number\n", num);
return 0;
}

运行结果:
[root@localhost soulmachine]# ./cha1
-9
-9 is odd number

gcc版本号:
gcc version 4.7.2 20121109 (Red Hat 4.7.2-8) (GCC)


Reply to this email directly or view it on GitHub
#13 (comment)
.

My tech blog: http://www.soulmachine.me
My GitHub: https://github.com/soulmachine
My LinkedIn: http://www.linkedin.com/in/soulmachine/
My Sina Weibo: http://weibo.com/soulmachine

Repository owner deleted a comment from guohaoyu110 Mar 1, 2024
Repository owner deleted a comment from guohaoyu110 Mar 1, 2024
Repository owner deleted a comment Mar 2, 2024
@github-staff github-staff deleted a comment from thangtq-pionero Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants