Go语言中文网 为您找到相关结果 7

leetcode刷题笔记(Golang)--11. Container With Most Water

题链接11. Container With Most Water Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. **Note: **You ...阅读全文

博文 2020-02-08 01:32:44 煮酒_zzh

leecode two sum golang解析

Leetcode上的two sum算法用golang实现 two sum问题 : Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. 解题一 一般思路: package main import ( "fmt" ) func twoSum(nums []int, target int) []int { for i, v1 := range nums { if i+1 != len(nums) { for j, v2 := range nums[i+1:] { if target == (v1 + v2) { return []int{i, i + j + 1...阅读全文

博文 2018-02-20 20:34:38 hades

【Leetcode】:22. Generate Parentheses 问题 in Go语言

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 解题思路:这道题和N-Queen问题非常类似,几乎是一样的解题模式。 首先需要明白,怎么放置括号是合法的,假设n=5的情况,已经合法的放置了4个括号,那么怎么判断下一个放什么括号合法呢? 放左括号:如果之前放置的左括号数>=n,那么一定不合法 放右括号:如果之前放置的左括号数<=之前放置的右括号数,那么...阅读全文

博文 2016-05-02 23:00:00 u013564276

leetcode刷题笔记(Golang)--14. Longest Common Prefix

题链接14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: ["flower","flow","flight"] Output: "fl" Example 2: Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. No...阅读全文

博文 2020-02-08 01:32:44 煮酒_zzh

leetcode刷题笔记(Golang)--16. 3Sum Closest

题链接16. 3Sum Closest Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example: Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2....阅读全文

博文 2020-02-08 01:32:41 煮酒_zzh

golang 解题标准括号问题

·解题思路来源https://github.com/azl3979858... package main import "container/list" import "fmt" func main(){ s := "{{}}" m := "{{[()}]}" l := "{[(())]}" fmt.Println(check(s)) fmt.Println(check(m)) fmt.Println(check(l)) } func check(x string) bool { mapper := map[byte]byte{ '{':'}', '(':')', '[':']', } stack := list.New() //初始化栈 for _, v := range x { i :=...阅读全文

博文 2020-05-28 11:32:45 氓浅

leetcode刷题笔记(Golang)--15. 3Sum

题链接15. 3Sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Example: Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ] 解题思路:转化为n个2sum问题 f...阅读全文

博文 2020-02-08 01:32:42 煮酒_zzh