LeetCode-2-add-two-numbers | 两数相加 发表于 2019-12-23 更新于 2022-04-27 分类于 LeetCode-Solutions 本文字数: 623 阅读时长 ≈ 1 分钟 题目链接 leetcode中国 解题思路12345678910111213141516171819202122232425262728293031323334/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { //先考虑特殊情况 if l1 == nil && l2 == nil { return nil } if l1 == nil { return l2 } if l2 == nil { return l1 } sum := l1.Val + l2.Val nextNode := addTwoNumbers(l1.Next, l2.Next) if sum < 10 { return &ListNode{ Val: sum, Next: nextNode } } else { tempNode := &ListNode{ Val: 1, Next: nil, } return &ListNode{ Val: sum - 10, Next: addTwoNumbers(nextNode, tempNode), } }}
Leetcode-292-nim-game | Nim 游戏 发表于 2019-12-19 更新于 2022-04-27 分类于 LeetCode-Solutions 本文字数: 131 阅读时长 ≈ 1 分钟 题目链接 leetcode中国 解题思路只要不被4整除就行。 123func canWinNim(n int) bool { return (n % 4 != 0)} 阅读全文 »
Leetcode-557-reverse-words-in-a-string-iii | 反转字符串中的单词 III 发表于 2019-12-19 更新于 2022-04-27 分类于 LeetCode-Solutions 本文字数: 594 阅读时长 ≈ 1 分钟 题目链接 leetcode中国 解题思路 1,先将字符串分解成数组 2,使用for range循环取出数组中的元素,并执行反转字符串的操作 3,将反转后的字符串append进一个新的string类型的数组 4,循环遍历这个新的string类型的数组,拼接成一个字符串,返回最终结果 代码 阅读全文 »
Leetcode-70-climbing-stairs | 爬楼梯 发表于 2019-12-17 更新于 2022-04-27 分类于 LeetCode-Solutions 本文字数: 532 阅读时长 ≈ 1 分钟 题目链接 leetcode中国 解题思路解法一第一层:0+1=1种第二层:1+1=2种第三层:2+1=3种第四层:3+2=5种第五层:5+3=8种第六层:8+5=13种… 阅读全文 »
LeetCode-9-palindrome-number | 回文数 发表于 2019-12-16 更新于 2022-04-27 分类于 LeetCode-Solutions 本文字数: 432 阅读时长 ≈ 1 分钟 题目链接 leetcode中国 解题思路第一种解法 1,先将x转为字符串类型,这样好根据下标来判断是否为回文数; 2,声明i和j两个int型变量,其中i在字符串x的最左边,j在字符串x的最右边,i从左往右开始递增,j从右往左开始递减,每次当i递增和j递减一次后,判断i和j下标所对应的元素是否相等,如果不相等,说明不是回文数,返回false,否则最终返回true。 阅读全文 »
Leetcode-136-single-number | 只出现一次的数字 发表于 2019-12-15 更新于 2022-04-27 分类于 LeetCode-Solutions 本文字数: 471 阅读时长 ≈ 1 分钟 题目链接 leetcode中国 leetcode 解题思路 第一步,新建一个map,用一个for循环,记录nums数组中出现的元素和次数,如果重复出现,value置为2,出现一次置为1; 第二步,再用一个for循环,遍历这个新建map,判断value为是否1,如果为1,就把value为1对应的key返回即可。 Golang 阅读全文 »
Leetcode-344-reverse-string | 反转字符串 发表于 2019-12-12 更新于 2022-04-27 分类于 LeetCode-Solutions 本文字数: 267 阅读时长 ≈ 1 分钟 题目链接 leetcode中国 解题思路第一步,使用for循环,将数组s里的最后一位append进新数组result,以此类推第二步,将新数组result里的值复制到数组s即可 Golang 阅读全文 »
Leetcode-78-subsets | 子集 发表于 2019-12-12 更新于 2022-04-27 分类于 LeetCode-Solutions 本文字数: 596 阅读时长 ≈ 1 分钟 题目链接 leetcode中国 解题思路12345678910111213141516171819202122//复制数组func copySlice(src []int) []int { temp := []int{} for _, i := range src { temp = append(temp, i) } return temp}func subsets(nums []int) [][]int { result := [][]int{} result = append(result, []int{}) //第一步 包含空集 for i := 0; i < len(nums); i++ { length := len(result) for j := 0; j < length; j++ { //设定两个下标元素,从0开始遍历nums的值 //里层复制数组,接着append进去下标为j的值组成nums的一个子集,再append进result数组 result = append(result, append(copySlice(result[j]), nums[i])) } } return result} 注意 阅读全文 »
Leetcode-53-maximum-subarray | 最大子序和 发表于 2019-12-10 更新于 2022-04-27 分类于 LeetCode-Solutions 本文字数: 651 阅读时长 ≈ 1 分钟 题目链接leetcode中文leetcode 解题思路暴力解12345678910111213141516func maxSubArray(nums []int) int { //一个临时值,和一个最大值 temp,max := 0,nums[0] for i:=0;i<len(nums);i++ { if (temp + nums[i]) > nums[i] { temp = temp + nums[i] } else { temp = nums[i] } if temp > max { max = temp } } return max //返回最大值} 阅读全文 »
LeetCode-7-reverse-integer | 整数反转 发表于 2019-12-08 更新于 2022-04-27 分类于 LeetCode-Solutions 本文字数: 675 阅读时长 ≈ 1 分钟 题目 leetcode中国 leetcode 题解通过题目可以看出来,这道题是让我们翻转一个整数,比如123,个位是1十位是2百位是3,反转后个位是3十位是2百位是1。 需要解决两个问题 阅读全文 »