Skip to content

Commit

Permalink
Merge pull request #1 from julycoding/master
Browse files Browse the repository at this point in the history
合并
  • Loading branch information
hejavac committed May 23, 2014
2 parents 164d159 + 26931be commit a98fd6d
Show file tree
Hide file tree
Showing 63 changed files with 3,747 additions and 3,690 deletions.
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
# Contributors
为示鼓励,所有贡献了本 github 的朋友,可以随时向 July 索取他博客内所有博文集锦的最新CHM文件,或任何一个系列的最新PDF或WORD,甚者,本系列集结出版成书后,愿赠送所有contributors 一人一本新书,以此感谢所有贡献的朋友:https://github.com/julycoding/The-Art-Of-Programming-by-July/graphs/contributors ,并非常期待你的加入,thanks。

集结令:欢迎所有已经贡献过本github的70位朋友加入程序员编程艺术室QQ群:149638123,验证信息为你贡献本项目时用的github昵称。
集结令:欢迎所有已经贡献过本github的80位朋友加入程序员编程艺术室QQ群:149638123,验证信息为你贡献本项目时用的github昵称。

孤军奋战的时代早已远去,我们只有团结起来,才能帮助到更多更无数的人。[@研究者July](http://weibo.com/julyweibo),始于二零一三年十二月十四日。

# Copyright
本电子书的版权属于July 等原作者们,严禁其他任何人出版,严禁用于任何商业用途,违者必究法律责任。July、二零一四年一月二十一日晨
本电子书的版权属于July 本人,严禁其他任何人出版,严禁用于任何商业用途,违者必究法律责任。July、二零一四年五月十一日晚

# July' PDF
* 支持向量机通俗导论(理解SVM的三层境界)Latex排版精细版:http://vdisk.weibo.com/s/zrFL6OXKgnlcp ;Latex版本②:https://raw.githubusercontent.com/liuzheng712/Intro2SVM/master/Intro2SVM.pdf
Expand Down
76 changes: 76 additions & 0 deletions ebook/code/c/2.1:寻找最小的 k 个数.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,79 @@ PARTITION(A, p, r) //partition过程 p为第一个数,r为最后一个


//...

//解法四示例
#include <stdio.h>
#define Cutoff ( 3 )

void swap( int *a, int *b )
{
int tmp = *a;
*a = *b;
*b = tmp;
}

int median3( int a[ ], int left, int right )
{
int Center = ( left + right ) / 2;

if( a[ left ] > a[ Center ] )
swap( &a[ left ], &a[ Center ] );
if( a[ left ] > a[ right ] )
swap( &a[ left ], &a[ right ] );
if( a[ Center ] > a[ right ] )
swap( &a[ Center ], &a[ right ] );

swap( &a[ Center ], &a[ right - 1 ] );
return a[ right - 1 ];
}
void insert_sort( int a[ ], int n )
{
int j, p, tmp;
for( p = 1; p < n; p++ )
{
tmp = a[ p ];
for( j = p; j > 0 && a[ j - 1 ] > tmp; j-- )
a[ j ] = a[ j - 1 ];
a[ j ] = tmp;
}
}

void q_select( int a[ ], int k, int left, int right )
{
int i, j;
int Pivot;

if( left + Cutoff <= right )
{
Pivot = median3( a, left, right );
//取三数中值作为枢纽元,可以消除最坏情况而保证此算法是O(N)
i = left; j = right - 1;
for( ; ; )
{
while( a[ ++i ] < Pivot ){ }
while( a[ --j ] > Pivot ){ }
if( i < j )
swap( &a[ i ], &a[ j ] );
else
break;
}
swap( &a[ i ], &a[ right - 1 ] ); /* 重置枢纽元 */

if( k <= i )
q_select( a, k, left, i - 1 );
else if( k > i + 1 )
q_select( a, k, i + 1, right );
}
else
insert_sort( a + left, right - left + 1 );
}

int main(int argc, const char *argv[])
{
int a[] = {0,2,1,3,4};
int k = 2;
q_select(a,k,0,5);
printf("%d\n",a[k-1]);
return 0;
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,32 @@
#include <cassert>
//二分查找V0.1实现版
//copyright@2011 July
//updated@2014-05-09 caopengcs

//首先要把握下面几个要点:
//right = n-1 => while(left <= right) => right = middle-1;
//right = n => while(left < right) => right = middle;
//middle的计算不能写在while循环外,否则无法得到更新。

int binary_search(int array[], int n, int value)
{
int left = 0;
int right = n - 1;
int binary_search(int array[], int n, int value) {
int left = 0,right = n - 1;
//如果这里是int right = n 的话,那么下面有两处地方需要修改,以保证一一对应:
//1、下面循环的条件则是while(left < right)
//2、循环内当 array[middle] > value 的时候,right = mid

while (left <= right) //循环条件,适时而变
{
while (left <= right) {
int middle = left + ((right - left) >> 1); //防止溢出,移位也更高效。同时,每次循环都需要更新。

if (array[middle] > value)
{
if (array[middle] > value) {
right = middle - 1; //right赋值,适时而变
}
else if(array[middle] < value)
{
else if (array[middle] < value) {
left = middle + 1;
}
else
else {
return middle;
}
//可能会有读者认为刚开始时就要判断相等,但毕竟数组中不相等的情况更多
//如果每次循环都判断一下是否相等,将耗费时间
}
return -1;
}

int main()
{
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
assert(binary_search(array, 10, 0) == 0);
assert(binary_search(array, 10, 5) == 5);
assert(binary_search(array, 10, 9) == 9);
assert(binary_search(array, 10, -1) == -1);
assert(binary_search(array, 10, 10) == -1);
return 0;
}
62 changes: 62 additions & 0 deletions ebook/code/go/1.5-回文判断.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//1.5 回文判断
//@author leiyonglin <leiyonglin@gmail.com>
//http://play.golang.org/p/K4NyYq-Teo
package main

import (
"fmt"
)

//解法一
func isPalindrome(s string) bool {
l := len(s)
if l < 1 {
return false
}

left := 0
right := l - 1
for left < right {
if s[left] != s[right] {
return false
}
left++
right--
}

return true
}

//解法二
func isPalindrome2(s string) bool {
l := len(s)
if l < 1 {
return false
}

m := l / 2
left := m - 1
right := m + l%2

for left >= 0 {
if s[left] != s[right] {
return false
}
left--
right++
}

return true
}

func main() {
s := "madam"

fmt.Printf("字符串%s是否为回文:%v \n", s, isPalindrome(s))
fmt.Printf("字符串%s是否为回文:%v \n", s, isPalindrome2(s))

s = "david"

fmt.Printf("字符串%s是否为回文:%v \n", s, isPalindrome(s))
fmt.Printf("字符串%s是否为回文:%v \n", s, isPalindrome2(s))
}
98 changes: 58 additions & 40 deletions ebook/code/php/chapter1.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,65 @@
<?php
/*
* Chapter One - Left Rotating String
* @author by howaichun
* @email: howaichun@foxmail.com
*/

/*
*method3
*/
function left_rotate3($string){
$arr_string = str_split($string);
$tmp_arr = array();
$num = count($arr_string);
for(;$num > 0; $num--){
$tmp_arr[] = array_pop($arr_string);
}
$rotate_string = implode($tmp_arr);
return $rotate_string;
function LeftShiftOne($string){
assert("is_string('$string')");
$len = mb_strlen($string,'utf-8');
return mb_substr($string,1,$len,'utf-8') . mb_substr($string,0,1,'utf-8');
}

/*
* method2
* @params string $string
*/
function left_rotate2($string){
$num = strlen($string);
$arr = array();
for($n = $num - 1;$n >= 0; $n--){
$arr[] = $string["$n"];
}
$rotate_string = implode("", $arr);
return $rotate_string;
function LeftShiftString($string,$m){
assert("is_string('$string')");
assert("is_int($m)");
$len = mb_strlen($string,'utf-8');
$m %= $len; //求余数,减少移动长度大于字符串长度时的比较次数
$m = $m<0 ? $m+$len : $m; //防止输入为负数导致while死循环
while($m--){
$string = LeftShiftOne($string);
}
return $string;
}

/*
* method1
* @params string $string left move string
*
*/
function left_rotate1($str){
$arr = str_split($str);
$result = array_reverse($arr);
$rotate_string = implode("", $result);
return $rotate_string;
function reverse($string){
assert("is_string('$string')");
return strrev($string);
}


function LeftRotateString($string,$m){
assert("is_string('$string')");
assert("is_int($m)");
$len = mb_strlen($string,'utf-8');
$m %= $len; //求余数,减少移动长度大于字符串长度时的比较次数
$head = reverse(mb_substr($string , 0 , $m , 'utf-8')); //反转[0..m - 1],套用到上面举的例子中,就是X->X^T,即 abc->cba
$tail = reverse(mb_substr($string , $m , $len , 'utf-8')); //反转[m..n - 1],例如Y->Y^T,即 def->fed
return reverse($head.$tail); //反转[0..n - 1],即如整个反转,(X^TY^T)^T=YX,即 cbafed->defabc
}

//"暴力移位法"测试用例
function LeftShiftTest($str){
var_dump(LeftShiftString($str , 0));
var_dump(LeftShiftString($str , 1));
var_dump(LeftShiftString($str , 6));
var_dump(LeftShiftString($str , 7));
var_dump(LeftShiftString($str , -1));
var_dump(LeftShiftString($str , -6));
var_dump(LeftShiftString($str , -7));
}

//"三步反转法"测试用例
function LeftRotateTest($str){
var_dump(LeftRotateString($str , 0));
var_dump(LeftRotateString($str , 1));
var_dump(LeftRotateString($str , 6));
var_dump(LeftRotateString($str , 7));
var_dump(LeftRotateString($str , -1));
var_dump(LeftRotateString($str , -6));
var_dump(LeftRotateString($str , -7));
}

//基本测试用例
$str = "abcdef";
LeftShiftTest($str);
LeftRotateString($str);
//中文utf8测试用例
$str = "一二三四五六";
LeftShiftTest($str);
LeftRotateString($str);
Loading

0 comments on commit a98fd6d

Please sign in to comment.