题目你现在手里有一份大小为 N x N 的『地图』(网格) grid,上面的每个『区域』(单元格)都用 0 和 1 标记好了。其中 0 代表海洋,1 代表陆地,你知道距离陆地区域最远的海洋区域是是哪一个吗?请返回该海洋区域到离它最近的陆地区域的距离。我们这里说的距离是『曼哈顿距离』( Manhattan Distance):(x0, y0) 和 (x1, y1) 这两个区域之间的距离是 |x0 - x1| + |y0 - y1| 。如果我们的地图上只有陆地或者海洋,请返回 -1。示例...
加一给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组。最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。你可以假设除了整数 0 之外,这个整数不会以零开头。示例 1:输入: [1,2,3]
输出: [1,2,4]
解释: 输入数组表示数字 123。示例 2:s输入: [4,3,2,1]
输出: [4,3,2,2]
解释: 输入数组表示数字 4321。这道题做了挺长时间, 主要是第一次的思路没有考虑到数组所表示的整数可能会溢出的情况. 傻傻...
Given a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition).[[0,0],[0,1],[1,1],[1,0]]Answer: True[[0,0],[0,10],[10,10],[10,0],[5,5]]Answer: False思路:这题问的是给一系列polygon的点,判断其是否是convex的。一个比较好的判断方法是看所有点是否是按照同一个顺序排列的,比如所有点都是依次顺时针,逆时针或者是同一条线排列。这里判断方向的方法...
Given a list of points that form a polygon when joined sequentially, find ifthis polygon is convex (Convex polygon definition).Note:There are at least 3 and at most 10,000 points.
Coordinates are in the range -10,000 to 10,000.
You may assume the polygon formed by given points is always a simple polygon (Simple polygon definition). In other words, we ensure that exactly two edges intersect at each...
1、在一个字符串里面找出最长的不重复子串
2、数组中的重复数字
第一种解法:先排序再扫描。从排好序的数组进行遍历,记录当前位置与其之前位置的数进行比较,若相等则输出该数。
时间复杂度:O(nlogn);空间复杂度O(1)
第二种解法:对数组进行遍历,每次判断哈希表中是否含有该元素,若有,输出此元素。若最后哈希表中的元素数量与数组中的相同,表面无重复数据。
时间复杂度:O(n);空间复杂度O(n)
3、给定一个数组代...
Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].Note:Although the above answer is in lexicographical order, your answer could be in any order you ...
LeetCode88. 合并两个有序数组Golang版
1. 问题描述
2. 思路
2.1. 思路1
声明一个新数组,最后再赋值给nums1
2.2. 思路2
从后向前填充
3. 代码
思路1代码
func merge(nums1 []int, m int, nums2 []int, n int) {var nums []int = make([]int, m+n)i := 0j := 0k := 0for i < m && j < n {if nums1[i] < nums2[j] {nums[k] = nums1[i]i++k++} else {nums[k] = nums2[j]j++k++}}nums = nums[0:k]if i >= m {nums = append(nums,nums...
LeetCode70. 爬楼梯Golang版
1. 问题描述
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
注意:给定 n 是一个正整数
2. 思路
到第n层的方法设为f(n),则f(n) = f(n - 1) + f(n - 2)
3. 代码
func climbStairs(n int) int {if n == 1 || n == 2 {return n}pre1 := 1pre2 := 2for i := 3; i <= n; i++ {temp := pre1 + pre2pre1 = pre2pre2 = temp} return pre2
}
LeetCode28. 实现strStr()Golang版
实现 strStr() 函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
2. 思路
3. 代码
func strStr(haystack string, needle string) int {if needle == "" {return 0}if len(needle) > len(haystack) {return -1}j := 0var ii intvar index int var length intfor i := 0; i < len(haystack); i...
LeetCode9. 回文数Golang版
1. 问题描述
给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。
2. 思路
转换为字符串,使用双指针遍历
3. 代码
func isPalindrome(x int) bool {if x > math.MaxInt32 || x < math.MinInt32 {return false}if x < 0 {return false}if x / 10 == 0 {return true} strX := strc...