[Leetcode]20天算法刷题计划之算法入门

Posted bfengj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Leetcode]20天算法刷题计划之算法入门相关的知识,希望对你有一定的参考价值。

前言

Leetcode里面的20天算法刷题计划中的算法入门部分的31道题,使用Go语言。

704. 二分查找

package question704

func Search(nums []int, target int) int 
	start,end := 0,len(nums)-1
	for start <= end 
		mid := (start+end)/2
		if nums[mid] == target 
			return mid
		else if nums[mid] < target 
			start = mid+1
		else 
			end = mid-1
		
	
	return -1

278. 第一个错误的版本

package question278

/**
 * Forward declaration of isBadVersion API.
 * @param   version   your guess about first bad version
 * @return 	 	      true if current version is bad
 *			          false if current version is good
 * func isBadVersion(version int) bool;
 */
func firstBadVersion(n int) int 
	start,end := 1,n
	for start <= end 
		mid := start + (end-start)/2
		if isBadVersion(mid) 
			end = mid-1
		else 
			start = mid + 1
		
	
	return start


35. 搜索插入位置

package question35


func searchInsert(nums []int, target int) int 
	start,end := 0,len(nums)-1
	for start <= end 
		mid := (start+end)/2
		if nums[mid] == target 
			return mid
		else if nums[mid] < target 
			start = mid+1
		else 
			end = mid-1
		
	
	return -1

977. 有序数组的平方

package question977

func sortedSquares(nums []int) []int 
	var index int = len(nums)-1
	res := make([]int,len(nums))
	left,right := 0,len(nums)-1
	for left<=right 
		if nums[left]*nums[left] >= nums[right]*nums[right] 
			res[index] = nums[left]*nums[left]
			left++
		else
			res[index] = nums[right]*nums[right]
			right--
		
		index--
	
	return res

189. 轮转数组

package question189


func rotate(nums []int, k int)  
	n := len(nums)
	k = k%n
	reverse(nums)
	reverse(nums[:k])
	reverse(nums[k:])


func reverse(nums []int)
	n := len(nums)
	for i:=0;i<n/2;i++
		temp := nums[i]
		nums[i]=nums[n-i-1]
		nums[n-i-1] = temp
	

283. 移动零

package question283

func moveZeroes(nums []int)  
	left,right,n := 0,0,len(nums)
	for right < n 
		if nums[right] != 0 
			nums[left],nums[right] = nums[right],nums[left]
			left++
		
		right++
	

167. 两数之和 II - 输入有序数组

package question167

func twoSum(numbers []int, target int) []int 
	n := len(numbers)
	left,right := 0,n-1

	for left<right 
		sum := numbers[left]+numbers[right]
		if sum==target 
			return []intleft+1,right+1
		else if sum<target 
			left++
		else 
			right--
		
	
	return []int

344. 反转字符串

package question344

func reverseString(s []byte)  
	n := len(s)
	for i:=0;i<(n>>1);i++
		s[i],s[n-i-1] = s[n-i-1],s[i]
	

557. 反转字符串中的单词 III

package question557

import "strings"

func reverseString(s []byte)  string
	n := len(s)
	for i:=0;i<(n>>1);i++
		s[i],s[n-i-1] = s[n-i-1],s[i]
	
	return string(s)

func reverseWords(s string) string 
	ss := strings.Split(s," ")
	for i:=0;i<len(ss);i++ 
		ss[i] = reverseString([]byte(ss[i]))
	
	return strings.Join(ss," ")

876. 链表的中间结点

package question876

type ListNode struct 
	Val  int
	Next *ListNode


func middleNode(head *ListNode) *ListNode 
	var leftNode,rightNode *ListNode = head,head
	for  
		if rightNode.Next==nil 
			return leftNode
		else
			rightNode = rightNode.Next.Next
			leftNode = leftNode.Next
		
		if rightNode==nil 
			return leftNode
		
	


19. 删除链表的倒数第 N 个结点

package question19

type ListNode struct 
	Val  int
	Next *ListNode

func removeNthFromEnd(head *ListNode, n int) *ListNode 
	hhead := &ListNode0,head
	left,right := hhead,head
	for i:=0;i<n;i++ 
		right = right.Next
	
	for ;right!=nil;left=left.Next 
		right = right.Next
	
	left.Next = left.Next.Next
	return hhead.Next


/*func removeNthFromEnd(head *ListNode, n int) *ListNode 
	left,right := head,head
	for i:=0;i<n;i++ 
		right = right.Next
	
	if right==nil 
		return head.Next
	
	for ;right.Next!=nil;left=left.Next 
		right = right.Next
	
	left.Next = left.Next.Next
	return head
*/

3. 无重复字符的最长子串

package question3

func max(x,y int) int
	if x>y
		return x
	else
		return y
	

/*func lengthOfLongestSubstring(s string) int 
	n := len(s)
	mmax := max(n,128)
	maxLength := 0
	left,right := 0,0
	for ;left<n;left++ 
		myMap := make(map[byte]int)
		for right=left;right<n&&right-left<mmax;right++ 
			if myMap[s[right]]>0 
				break
			
			myMap[s[right]]++
		
		maxLength = max(maxLength,right-left)
		if right==n 
			return maxLength
		
		left += strings.Index(string([]byte(s)[left:right]), string(s[right]))
		fmt.Println(left)
	
	return maxLength
*/
func lengthOfLongestSubstring(s string) int 
	n := len(s)
	mmax := max(n,128)
	maxLength := 0
	myMap := make(map[byte]int)
	left,right := 0,0
	for ;left<n;left++ 
		if left != 0 
			myMap[s[left-1]]--
		
		for ;right<n&&right-left<mmax;right++ 
			if myMap[s[right]]>0 
				break
			
			myMap[s[right]]++
		
		maxLength = max(maxLength,right-left)
	
	return maxLength

567. 字符串的排列

package question567

import "fmt"

func checkInclusion(s1 string, s2 string) bool 
	n1, n2 := len(s1), len(s2)
	if n1>n2 
		return false
	
	myMap := make(map[byte]int)
	for i := 0; i < n1; i++ 
		myMap[s1[i]]++
		myMap[s2[i]]--
	
	var diff int = 0
	for _,k :=range myMap
		if k!=0 
			diff++
		
	
	if diff==0 
		return true
	

	for i := n1; i < n2; i++ 
		if myMap[s2[i-n1]]==0 
			diff++
		
		myMap[s2[i-n1]]++
		if myMap[s2[i-n1]]==0 
			diff--
		
		if myMap[s2[i]]==0 
			diff++
		
		myMap[s2[i]]--
		if myMap[s2[i]]==0 
			diff--
		
		fmt.Println(diff)
		if diff == 0 
			return true
		
	
	return false


/*func checkInclusion(s1 string, s2 string) bool 
	n1, n2 := len(s1), len(s2)
    if n1>n2 
        return false
    
	map1 := make(map[byte]int)
	map2 := make(map[byte]int)
	for i := 0; i < n1; i++ 
		map1[s1[i]]++
		map2[s2[i]]++
	
	var fflag bool = true
	for k, v := range map1 
		if map2[k]!=v 
			fflag = false
		
	
	if fflag==true 
		return true
	
    fmt.Println(map1)
	for i := 1; i < n2-n1+1; i++ 
		map2[s2[i-1]]--
		map2[s2[i+n1-1]]++
        fmt.Println(map1)
		var flag bool = true
		for k, v := range map1 
			if map2[k]!=v 
				flag = false
			
		
		if flag==true 
			return true
		
	
	return false


*/

733. 图像渲染

package question733

//BFS
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int 
	target := image[sr][sc]
	if target == newColor 
		return image
	
	image[sr][sc] = newColor
	queue := [][]int
	xx := []int-1, 0, 0, 1
	yy := []int0, -1, 1, 0
	queue = append(queue, []intsr, sc)
	for i := 0; i < len(queue); i++ 
		nowEle := queue[i]
		for j := 0; j < 4; j++ 
			mx, my := nowEle[0]+xx[j], nowEle[1]+yy[j]
			if mx >= 0 && my >= 0 && mx < len(image) && my < len(image[0]) && image[mx][my] == target 
				image[mx][my] = newColor
				queue = append(queue, []intmx, my)
			
		
	
	return image


695. 岛屿的最大面积

package question695

func maxAreaOfIsland(grid [][]int) int 
	res := 0
	xx := []int-1, 0, 0, 1
	yy := []int0, -1, 1, 0
	for i := 0;i<len(grid);i++ 
		for j := 0;j<len(grid[i]);j++ 
			if grid[i][j]==0 
				continue
			
			nowArea := 1
			queue := [][]int
			queue = append(queue,[]inti,j)
			grid[i][j] = 0
			for k := 0; k < len(queue);k++ 
				nowEle := queue[k]
				for p := 0; p < 4; p++ 
					mx, my := nowEle[0]+xx[p], nowEle[1]+yy[p]
					if mx >= 0 && my >= 0 && mx < len(grid) && my < len(grid[0]) && grid[mx][my] != 0 
						grid[mx][my] = 0
						nowArea++
						queue = append(queue, []intmx, my)
					
				
			
			if nowArea>res 
				res = nowArea
			
		
	
	return res

617. 合并二叉树

package question617

type TreeNode struct 
	Val   int
	Left  *TreeNode
	Right *TreeNode

//深度优先搜索
func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode 
	if root1==nil 
		return root2
	else if root2==nil 
		return root1
	
	root1.Val = root1.Val+root2.Val
	root1.Left = mergeTrees(root1.Left,root2.Left)
	root1.Right = mergeTrees(root1.Right,root2.Right)
	return root1


116. 填充每个节点的下一个右侧节点指针

package question116

type Node struct 
	Val   int
	Left  *Node
	Right *Node
	Next  *Node


func connect(root *Node) *Node 
	if root == nil 
		return root
	
	if root.Left != nil 
		root.Left.Next = root
		root.Right.Next = root
	
	if root.Next != nil 
		if root.Next.Right != root 
			root.Next = root.Next.Right
		 else if root.Next.Next == nil 
			root.Next = nil
		 else 
			root.Next = root.Next.Next.Left
		
	
	connect(root.Left)
	connect(root.Right)
	return root


542. 01 矩阵

package question542


func updateMatrix以上是关于[Leetcode]20天算法刷题计划之算法入门的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 20天算法刷题计划第三天:双指针

算法进阶之Leetcode刷题记录

LeetCode刷题计划

LeetCode刷题计划

LeetCode刷题计划

LeetCode刷题计划