博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode Weekly Contest 21
阅读量:7125 次
发布时间:2019-06-28

本文共 2694 字,大约阅读时间需要 8 分钟。

Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.

Example 1:

Input: [23, 2, 4, 6, 7], k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
Example 2:
Input: [23, 2, 6, 4, 7], k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
Note:
The length of the array won't exceed 10,000.
You may assume the sum of all the numbers is in the range of a signed 32-bit integer.

Can use Sum[n] = (a[0]+...a[n]) to reduce the time complexity

Payattention to the module K, border check

func checkSubarraySum(nums []int, k int) bool {    sum := make([]int, 0, len(nums))    sum = append(sum, 0)    for index, num := range nums {        if 0 == index {            sum = append(sum, num)        } else {            sum = append(sum, sum[len(sum)-1] + num)        }    }        //fmt.Printf("%v\n", sum)    sumLen := len(sum)    for beg:= 0; beg < sumLen; beg++ {        for end:= beg+2; end < sumLen; end++ {            if k == 0 {                if sum[end] - sum[beg] == 0 {                    return true                }            } else {                if (sum[end] - sum[beg]) % k == 0 {                    return true                }            }        }      }        return false}

Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.

Example 1:

Input:
s = "abpcplea", d = ["ale","apple","monkey","plea"]

Output:

"apple"
Example 2:
Input:
s = "abpcplea", d = ["a","b","c"]

Output:

"a"
Note:
All the strings in the input will only contain lower-case letters.
The size of the dictionary won't exceed 1,000.
The length of all the strings in the input won't exceed 1,000.

func match(s, t string) bool {    ti := 0    for si := 0; si < len(s); si++ {        if t[ti] == s[si] {            ti++            if ti == len(t) {                return true            }        }    }        return false}func findLongestWord(s string, d []string) string {    res := ""    //fmt.Printf("%v\n", "aaaa" > "aaab")    for _, t := range d {        if len(t) >= len(res) {            if true == match(s, t) {                if len(t) == len(res) && t > res {                    continue                }                res = t            }        }    }        return res}

转载地址:http://bteel.baihongyu.com/

你可能感兴趣的文章
SVN入门及配置使用
查看>>
Java IO类库之ByteArrayOutputStream
查看>>
《Spring5学习》04 - 面向切面编程
查看>>
druid简单教程
查看>>
推荐一款免费好用的网页视频播放器
查看>>
Android 保活
查看>>
小程序环境搭建与开发工具的简单介绍
查看>>
我的友情链接
查看>>
red hat5安装mysql5.5.25
查看>>
深入理解C++的动态绑定和静态绑定
查看>>
cisco 7200 simulator
查看>>
JAVA WEB搭建 SpringMVC+Spring+hibernate 框架
查看>>
HibernateTemplate中常用的方法
查看>>
clang: error: unknown argument: 'websockets'解决方法
查看>>
Vue.js 特有的一种ajax——axios
查看>>
我的友情链接
查看>>
dos2unix命令 - 将DOS格式文本文件转换成UNIX格式
查看>>
[置顶] 全屏打开网页
查看>>
windows下利用cwRsync批量更新
查看>>
github工作流程之三---发起一个pull 请求
查看>>