-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from julycoding/master
合并
- Loading branch information
Showing
63 changed files
with
3,747 additions
and
3,690 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.