'天降字符串转整数'

This commit is contained in:
朱毅骏 2021-04-01 18:12:09 +08:00
parent b5a2852bb0
commit 552889ff24
8 changed files with 426 additions and 7 deletions

View File

@ -4,7 +4,7 @@
* @Autor: zhuyijun
* @Date: 2021-03-29 09:08:50
* @LastEditors: zhuyijun
* @LastEditTime: 2021-03-29 17:35:47
* @LastEditTime: 2021-03-30 08:59:23
*/
package main
@ -14,20 +14,110 @@ import "fmt"
二分查找算法
*/
func BinarySearch(key int, a []int) int {
lo := 0
hi := len(a) - 1
for lo < hi {
mid := lo + (hi-lo)/2
start := 0
end := len(a) - 1
for start < end {
mid := start + ((end - start) >> 1)
if key < a[mid] {
hi = mid - 1
end = mid - 1
} else if key > a[mid] {
lo = mid + 1
start = mid + 1
} else {
return mid
}
}
return -1
}
//查找第一个值等于给定值得元素
func BinarySearch2(nums []int, value int) int {
length := len(nums)
start := 0
end := length - 1
for start <= end {
mid := start + ((end - start) >> 1)
if nums[mid] > value {
end = mid - 1
} else if nums[mid] < value {
start = mid + 1
} else {
for mid >= 0 {
if nums[mid-1] == value {
mid--
} else {
return mid
}
}
}
}
return -1
}
// 查找最后一个值等于给定值的元素
func BinarySearch3(nums []int, value int) int {
start := 0
end := len(nums) - 1
for start <= end {
mid := start + ((end - start) >> 1)
if nums[mid] > value {
end = mid - 1
} else if nums[mid] < value {
start = mid + 1
} else {
for mid < len(nums) {
if nums[mid+1] == value {
mid++
} else {
return mid
}
}
}
}
return -1
}
// 查找第一个大于等于给定值的元素
func BinarySearch4(nums []int, value int) int {
start := 0
end := len(nums) - 1
for start <= end {
mid := (start + end) >> 1
if nums[mid] < value {
start = mid + 1
} else {
for mid >= 0 {
if nums[mid-1] >= value {
mid--
} else {
return mid
}
}
}
}
return -1
}
// 查找最后一个小于等于给定值的元素
func BinarySearch5(nums []int, value int) int {
start := 0
end := len(nums) - 1
for start <= end {
mid := (start + end) >> 1
if nums[mid] > value {
end = mid - 1
} else {
for mid < len(nums) {
if nums[mid+1] <= value {
mid++
} else {
return mid
}
}
}
}
return -1
}
func main() {
var a = []int{0, 2, 4, 7, 9, 11, 45, 67, 99, 100}
var i int = 2

View File

@ -0,0 +1,9 @@
/*
* @Description:
* @Version: 2.0
* @Autor: zhuyijun
* @Date: 2021-03-30 09:01:29
* @LastEditors: zhuyijun
* @LastEditTime: 2021-03-30 09:01:49
*/
package array

View File

@ -0,0 +1,69 @@
package main
import (
"math/big"
"strconv"
)
func myAtoi(s string) int {
const MIN int64 = -2147483648
const MAX int64 = 2147483647
ret := big.NewInt(0)
//正负号
flag := true
size := len(s)
i := 0
if s == "" {
return 0
}
//检查空格
for ; i < size; i++ {
if s[i] != ' ' {
break
}
}
if i >= size {
return 0
}
//检查正负号;
if s[i] == '-' || s[i] == '+' {
if s[i] == '-' {
flag = false
} else {
flag = true
}
i++
}
//检查数字
for ; i < size; i++ {
//不是数字就跳出
if s[i] > '9' || s[i] < '0' {
break
} else {
n, err := strconv.ParseInt(string(s[i]),10,64)
if err != nil {
break
}
ret.Mul(ret,big.NewInt(10))
ret.Add(ret,big.NewInt(n))
}
}
//添加符号
if flag == false {
ret.Neg(ret)
}
if ret.Cmp(big.NewInt(MIN)) < 0 {
ret = big.NewInt(MIN)
}
if ret.Cmp(big.NewInt(MAX)) > 0 {
ret = big.NewInt(MAX)
}
return int(ret.Int64())
}
func main() {
print(myAtoi("9223372036854775808"))
}

View File

@ -0,0 +1,16 @@
package main
import "strings"
func myAtoi(s string) int {
//去除首尾字符
s = strings.TrimSpace(s)
if len(s) == 0 {
return 0
}
return 0
}
func main() {
print(myAtoi("9223372036854775808"))
}

View File

@ -0,0 +1,102 @@
/*
* @Description:
* @Version: 2.0
* @Autor: zhuyijun
* @Date: 2021-03-31 10:57:16
* @LastEditors: zhuyijun
* @LastEditTime: 2021-03-31 11:07:30
*/
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
package main
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
/**
输入一个链表的头节点从尾到头反过来返回每个节点的值用数组返回
示例 1
输入head = [1,3,2]
输出[2,3,1]
限制
0 <= 链表长度 <= 10000
*/
//方法1 回溯
func reversePrint(head *ListNode) []int {
res := make([]int,0)
var reverse func(*ListNode)
reverse = func(node *ListNode) {
if node.Next != nil {
reverse(node.Next)
}
res = append(res, node.Val)
}
if head == nil {
return nil
}
reverse(head)
return res
}
/**
方法二 一般递归
*/
func reversePrint2(head *ListNode) []int {
if head == nil {
return nil
}
var nums []int
nums = readList(head,nums)
len :=len(nums)
for i:= 0; i < len >> 1; i++{
temp := nums[i];
nums[i] = nums[len - i - 1];
nums[len - i - 1] = temp;
}
return nums
}
func readList(head *ListNode,nums []int)[]int {
nums = append(nums, head.Val)
print(head.Val)
if head.Next == nil {
return nums
}
return readList(head.Next,nums)
}
/**
递归回溯
*/
func reversePrint3(head *ListNode) []int {
if head == nil {
return nil
}
var nums []int
readList3(head,&nums)
return nums
}
func readList3(head *ListNode,nums* []int) {
if head.Next != nil {
readList3(head.Next,nums)
}
*nums = append(*nums, head.Val)
}
func main() {
l := &ListNode{2, &ListNode{1, &ListNode{3, nil}}}
fmt.Printf("%v",reversePrint3(l))
}

View File

@ -0,0 +1,120 @@
package main
import (
"container/list"
"fmt"
)
/*
用两个栈实现一个队列队列的声明如下请实现它的两个函数 appendTail deleteHead
分别完成在队列尾部插入整数和在队列头部删除整数的功能
(若队列中没有元素deleteHead操作返回 -1 )
示例 1
输入
["CQueue","appendTail","deleteHead","deleteHead"]
[[],[3],[],[]]
输出[null,null,3,-1]
示例 2
输入
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
输出[null,-1,null,null,5,2]
提示
1 <= values <= 10000
最多会对appendTaildeleteHead 进行10000次调用
*/
type CQueue struct {
//入
stack1 []int
//出
stack2 []int
}
func Constructor() CQueue {
return CQueue{stack1: []int{}, stack2: []int{}}
}
func (this *CQueue) AppendTail(value int) {
this.stack1 = append(this.stack1[:], value)
}
func (this *CQueue) DeleteHead() int {
if len(this.stack2) == 0 && len(this.stack1) == 0 {
return -1
}else if len(this.stack2) == 0 && len(this.stack1) > 0 {
l := len(this.stack1) - 1
for len(this.stack1) > 0 {
val := this.stack1[l]
this.stack1 = this.stack1[:l]
l--
this.stack2 = append(this.stack2,val)
}
}
r := this.stack2[len(this.stack2) - 1]
this.stack2 = this.stack2[:len(this.stack2) - 1]
return r
}
//方法二
type CQueue2 struct {
stack1, stack2 *list.List
}
func Constructor2() CQueue2 {
return CQueue2{
stack1: list.New(),
stack2: list.New(),
}
}
func (this *CQueue2) AppendTail2(value int) {
this.stack1.PushBack(value)
}
func (this *CQueue2) DeleteHead2() int {
// 如果第二个栈为空
if this.stack2.Len() == 0 {
for this.stack1.Len() > 0 {
this.stack2.PushBack(this.stack1.Remove(this.stack1.Back()))
}
}
if this.stack2.Len() != 0 {
e := this.stack2.Back()
this.stack2.Remove(e)
return e.Value.(int)
}
return -1
}
func main() {
obj := Constructor()
obj.AppendTail(3)
obj.AppendTail(2)
obj.AppendTail(3)
obj.AppendTail(5)
obj.AppendTail(7)
param_2 := obj.DeleteHead()
fmt.Println(param_2)
param_2 = obj.DeleteHead()
fmt.Println(param_2)
param_2 = obj.DeleteHead()
fmt.Println(param_2)
}
/**
* Your CQueue object will be instantiated and called as such:
* obj := Constructor();
* obj.AppendTail(value);
* param_2 := obj.DeleteHead();
*/

View File

@ -0,0 +1,13 @@
package main
/**
请实现一个函数用来判断字符串是否表示数值包括整数和小数
例如字符串"+100""5e2""-123""3.1416""-1E-16""0123"都表示数值
"12e""1a3.14""1.2.3""+-5""12e+5.4"都不是
*/
func main() {
}