1029. 可被 5 整除的二进制前缀  显示英文描述 我的提交返回竞赛   用户通过次数467 用户尝试次数662 通过次数477 提交次数1964 题目难度Easy 给定由若干 0 和 1 组成的数组 A.我们定义 N_i:从 A[0] 到 A[i] 的第 i 个子数组被解释为一个二进制数(从最高有效位到最低有效位). 返回布尔值列表 answer,只有当 N_i 可以被 5 整除时,答案 answer[i] 为 true,否则为 false. 示例 1: 输入:[0,1,1] 输出:[tr…
Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a binary number (from most-significant-bit to least-significant-bit.) Return a list of booleans answer, where answer[i] is true if and only if N_i is divi…
网址:https://leetcode.com/problems/binary-prefix-divisible-by-5/ 一次for循环遍历数组,在上次计算的基础上得到本次的结果! class Solution { public: vector<bool> prefixesDivBy5(vector<int>& A) { vector<bool> ans; ; ; ; i<A.size(); i++) { num = ; num += pre_num…
[JavaScript]Leetcode每日一题-最大整除子集 [题目描述] 给你一个由 无重复 正整数组成的集合 nums ,请你找出并返回其中最大的整除子集 answer ,子集中每一元素对(answer[i], answer[j])都应当满足: answer[i] % answer[j] == 0 ,或 answer[j] % answer[i] == 0 如果存在多个有效解子集,返回其中任何一个均可. 示例1: 输入:nums = [1,2,3] 输出:[1,2] 解释:[1,3] 也会…
A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Also, a self-dividing number is not allowed to contain the digit zero. G…
1022. 可被 K 整除的最小整数  显示英文描述 我的提交返回竞赛   用户通过次数74 用户尝试次数262 通过次数75 提交次数1115 题目难度Medium 给定正整数 K,你需要找出可以被 K 整除的.仅包含数字 1 的最小正整数 N. 返回 N 的长度.如果不存在这样的 N,就返回 -1. 示例 1: 输入:1 输出:1 解释:最小的答案是 N = 1,其长度为 1. 示例 2: 输入:2 输出:-1 解释:不存在可被 2 整除的正整数 N . 示例 3: 输入:3 输出:3 解释…
给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" 输出: "100" 示例 2: 输入: a = "1010", b = "1011" 输出: "10101" class Solution: def addBinary(self, a, b): """ :…
地址 https://www.acwing.com/solution/leetcode/content/6340/ 题目描述给你一个整数数组 nums,请你找出并返回能被三整除的元素最大和. 示例 : 输入:nums = [,,,,] 输出: 解释:选出数字 , , 和 ,它们的和是 (可被 整除的最大和). 示例 : 输入:nums = [] 输出: 解释: 不能被 整除,所以无法选出数字,返回 . 示例 : 输入:nums = [,,,,] 输出: 解释:选出数字 , , 以及 ,它们的和是…
题目:给你一个整数数组 nums,请你找出并返回能被三整除的元素最大和. 示例 1: 输入:nums = [3,6,5,1,8] 输出:18 解释:选出数字 3, 6, 1 和 8,它们的和是 18(可被 3 整除的最大和). 这道题是第163周竞赛的一道题目,难度中等,边学别人的解题方法,边记录吧! 方案一.不妨设dp[i] 代表 选取的数字累加和 模3 = i 的数字和 假定nums[i] % 3 = 1 ,那么,和 前面选取的数字和模 3 = 2 的数相加,就可以模3为 0 ,表达起来就是…
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6], 2…
Given two binary strings, return their sum (also a binary string).The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010&qu…
官方题解 作差排序 描述 公司计划面试 2N 人.第 i 人飞往 A 市的费用为 costs[i][0],飞往 B 市的费用为 costs[i][1]. 返回将每个人都飞到某座城市的最低费用,要求每个城市都有 N 人抵达. 示例: 输入:[[10,20],[30,200],[400,50],[30,20]]输出:110解释:第一个人去 A 市,费用为 10.第二个人去 A 市,费用为 30.第三个人去 B 市,费用为 50.第四个人去 B 市,费用为 20. 最低总费用为 10 + 30 + 5…
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs are consist of lowercase letters a-z. 这道题让我们实现一个重要但又有些复杂的数据结构-字典树, 又称前缀树或单词查找树,详细介绍可以参见网友董的博客,例如,一个保存了8个键的trie结构,"A", "to", "tea&quo…
这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow","flight"] 输出: "fl" 示例 2: 输入: ["dog","racecar","car"] 输出: "" 解释: 输入…
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" Exa…
题目 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1,1] 为两种不同的情况. 说明 : 数组的长度为 [1, 20,000]. 数组中元素的范围是 [-1000, 1000] ,且整数 k 的范围是 [-1e7, 1e7]. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/subarray-sum-eq…
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子串,假设第一个字符串是最长前缀子串,采用增强for得到数组中每个字符串,分别与第一个字符串的同一位置进行比较,有一个字符串在该位置不一致,就返回. public class Solution { public String longestCommonPrefix(String[] strs) { i…
二分法+前缀和法律 满足子序列长度的条件(0,n)之间,sum[x+i]-sum[i]从i元素开始序列长度x和.前缀和可在O(n)的时间内统计 sum[i]的值.再用二分找出满足条件的最小的子序列长度. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #include&…
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" Exa…
Easy! 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow","flight"] 输出: "fl" 示例 2: 输入: ["dog","racecar","car"] 输出: "" 解释: 输入不存在公共前缀. 说明: 所有…
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow","flight"]输出: "fl" 示例 2: 输入: ["dog","racecar","car"]输出: "" 解释: 输入不存在公共前缀.说明: 所有输入只包含小写字母 a-z .…
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" Exa…
题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow","flight"] 输出: "fl" 示例 2: 输入: ["dog","racecar","car"] 输出: "" 解释: 输入不存在公共前缀. 说明: 所有输入只包含小写…
[题目描述] 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow","flight"]输出: "fl" 示例 2: 输入: ["dog","racecar","car"]输出: ""解释: 输入不存在公共前缀.说明: 所有输入只包含小写字母…
题意:有两个\(01\)字符串\(a\)和\(b\),每次让\(a\)和\(b\)进行与运算,将值贡献给答案,然后将\(b\)右移一位,直到\(b=0\). 题解:因为\(a\)不变,而\(b\)每次右移一位,所以我们看\(b\)中\(1\)的位置在\(a\)中所对应的位置,从该位置到最低位,所有为\(1\)的位置都要算一次十进制的数贡献给答案,那么为了降低复杂度,很明显,我们使用前缀和,用十进制记录\(a\)中从低位到高位的和,然后再从低位到高位遍历\(b\),累加所有\(1\)位置在\(a\…
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如:[Swift]LeetCode156.二叉树的上下颠倒 $ Binary Tree Upside Down 请下拉滚动条查看最新 Weekly Contest!!! Swift LeetCode 目录 | Catalog 序        号 题名Title 难度     Difficulty  两数之…
本期共7道题,三道简单题,四道中等题. 此部分题目是作者认为有价值去做的一些题,但是其考察的知识点不在前三篇总结系列里面. 例1解法:采用数组索引位置排序的思想. 例2解法:考察了组合数学的组合公式应用.有点类似动态规划的思想. 例3解法:  考察灵活运用二进制和整除余数的数学知识. 例4解法:动态规划思想的应用. 例5解法:分类讨论的数学思想. 例6解法:考察灵活运用哈希字典. 例7解法:考察闰年的数学判断公式,计算周几的细节处理能力. 例1 最大宽度坡 题号:962,难度:中等 题目描述:…
算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 0-1 背包 数组区间 字符串编辑 其它问题 数学 素数 最大公约数 进制转换 阶乘 字符串加法减法 相遇问题 多数投票问题 其它 数据结构相关 栈和队列 哈希表 字符串 数组与矩阵 1-n 分布 有序矩阵 链表 树 递归 层次遍历 前中后序遍历 BST Trie 图 位运算 参考资料 算法思想 二…
/* * @lc app=leetcode.cn id=67 lang=c * * [67] 二进制求和 * * https://leetcode-cn.com/problems/add-binary/description/ * * algorithms * Easy (46.67%) * Total Accepted: 17.6K * Total Submissions: 37.8K * Testcase Example: '"11"\n"1"' * * 给定两…
https://oj.leetcode.com/problems/add-binary/ 对两个二进制字符串求和. 对于字符串 ans_str,求它的翻转. reverse(ans_str.begin(), ans_str.end()) 也可以: s.assign(ans_str.rbegin(),ans_str.rend()) class Solution { public: string addBinary(string a, string b) { if(a.empty()) return…