为了访问Java中String的单个字符,我们有String.charAt(2).是否有任何内置函数可以删除java中字符串的单个字符?
像这样的东西:
if(String.charAt(1) == String.charAt(2){
//I want to remove the individual character at index 2.
}
Run Code Online (Sandbox Code Playgroud) 我试图回答以下问题:你有一个整数数组,这样每个整数都有一个奇数个时间,除了3个.找到三个数字.
到目前为止,我带来了蛮力方法:
public static void main(String[] args) {
// TODO Auto-generated method stub
int number[] = { 1, 6, 4, 1, 4, 5, 8, 8, 4, 6, 8, 8, 9, 7, 9, 5, 9 };
FindEvenOccurance findEven = new FindEvenOccurance();
findEven.getEvenDuplicates(number);
}
// Brute force
private void getEvenDuplicates(int[] number) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i : number) {
if (map.containsKey(i)) {
// a XOR a XOR a ---- - -- - - odd times = …Run Code Online (Sandbox Code Playgroud) algorithm bit-manipulation time-complexity space-complexity data-structures
假设我有一个尺寸为N(N <= 50)的方阵,相邻的元素不包括对角线.
在给定M的情况下,如何找到M个相邻元素之间的最大总和?
例如,将此矩阵4x4:
Matrix: For M = 3 For M = 4
3 1 5 2 3 1 5 2 3 1 5 2
2 6 1 3 2 [6] 1 3 2 [6] 1 3
1 4 4 2 1 [4][4] 2 1 [4] 4 2
5 3 2 7 5 3 2 7 [5][3] 2 7
Biggest = 14 Biggest = 18
Run Code Online (Sandbox Code Playgroud)
我试着这样做,但经过一定的维度,它很慢.
#include <bits/stdc++.h>
using namespace std;
int mat[51][51];
int mark[51][51];
int m, n;
int …Run Code Online (Sandbox Code Playgroud) 我知道使用自下而上动态编程方法的解决方案在O(n ^ 2)中解决了这个问题.我特意寻找自上而下的dp方法.是否有可能使用递归解决方案实现最长的回文子串?
这是我尝试过的但是在某些情况下它失败了,但我觉得我差不多正常.
#include <iostream>
#include <string>
using namespace std;
string S;
int dp[55][55];
int solve(int x,int y,int val)
{
if(x>y)return val;
int &ret = dp[x][y];
if(ret!=0){ret = val + ret;return ret;}
//cout<<"x: "<<x<<" y: "<<y<<" val: "<<val<<endl;
if(S[x] == S[y])
ret = solve(x+1,y-1,val+2 - (x==y));
else
ret = max(solve(x+1,y,0),solve(x,y-1,0));
return ret;
}
int main()
{
cin >> S;
memset(dp,0,sizeof(dp));
int num = solve(0,S.size()-1,0);
cout<<num<<endl;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试优化算法,我想不出更好的方法来做到这一点.
有一个输入(时钟频率值)将通过乘数和除数的组合.
OutClk =(InClk*Mult1*Mult2*Mult3*Mult4/Div1)/ Div2
我目前的(天真的?)实现是:
#define PRE_MIN 10000000
#define PRE_MAX 20000000
// Available values of the multipliers and divisors.
uint8_t mult1_vals[] = {1, 2};
uint8_t mult2_vals[] = {1, 2, 4, 8};
uint8_t mult3_vals[] = {3, 5, 7};
uint8_t div1_vals[] = {1, 2, 4};
uint8_t div2_vals[] = {1, 2, 4, 8};
bool exists_mults_divs(uint32_t in_val, uint32_t out_val)
{
uint8_t i_m1, i_m2, i_m3, i_d1, i_d2;
uint32_t calc_val;
for (i_m1 = 0; i_m1 < sizeof(mult1_vals); i_m1++) {
for (i_m2 = 0; i_m2 …Run Code Online (Sandbox Code Playgroud) 以下是hackerearth提出的问题.这是问题问题的链接 !我在java和c中编写了它的解决方案,但是在提交的某些测试用例中超出了时间限制.没有参与者能够为所有测试用例解决这个问题.什么是最有效的解决方案?
题:
鲍勃喜欢DSD Numbers.DSD Number是一个可以用十进制表示的数字和整除的数字.
digitSum(n):n的位数之和(以十进制表示形式)
例如:n = 1234然后digitSum(n)= 1 + 2 + 3 + 4 = 10
DSD Number是数字n,使得n%digitSum(n)等于0
Bob要求Alice告诉范围[L,R]的DSD号码的数量.
约束:
1 <=测试用例<= 50
1 <= L <= R <= 10 ^ 9
样本输入
4 2 5 1 10 20 45 1 100
样本输出
4 10 9 33
Java中的代码:
class DSD {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);
int t=Integer.parseInt(br.readLine());
while(t-->0){
StringTokenizer st=new StringTokenizer(br.readLine());
int …Run Code Online (Sandbox Code Playgroud) 我的书为函数提供了以下代码,该函数计算一串唯一字符的所有排列(请参见下面的代码),并说运行时间为O(n!),因为存在n!个排列。
我不明白他们如何将运行时间计算为O(n!)。我认为它们的意思是“ n”是原始字符串的长度。我认为运行时间应该类似于O((n + 1)XY),因为getPerms函数将被调用(n + 1)次,并且X和Y可以表示外部和内部for循环的运行时间分别。有人可以向我解释为什么这是错误的/书的答案是正确的吗?
谢谢。
public static ArrayList<String> getPerms(String str)
{
if (str == null)
return null;
ArrayList<String> permutations = new ArrayList<String>();
if (str.length() == 0)
permutations.add("");
return permutations;
char first = str.charAt(0); //first character of string
String remainder = str.substring(1); //remove first character
ArrayList<String> words = getPerms(remainder);
for (String word: words)
{
for (i = 0; i <= word.length(); i++)
{
String s = insertCharAt(word, first, i);
permutations.add(s)
}
}
return permutations;
}
public static String …Run Code Online (Sandbox Code Playgroud) 我试图做练习题,但遇到了一个我不明白其背后推理的解决方案。
问题可以在这里找到,求偶数和子数组的个数。 https://www.geeksforgeeks.org/find-number-subarrays-even-sum/
已提出相关问题,但我具体询问解决方案末尾握手引理的使用。
我知道我们构建了偶数和奇数和子数组的计数,但不明白为什么我们使用握手引理来计算偶数和子数组的数量。如果我们得到偶数和奇数累积和的计数,那么握手引理到底是如何发挥作用的呢?显然,偶数和子数组由奇数 + 奇数、偶数 + 偶数或单个偶数值组成,所以我只想知道在这个特定场景中如何准确地解释所有情况。感谢您的帮助!
让 = (, ) 是一个有边权重的有向图,让 是 的顶点。所有的边权重都是 1 到 20 之间的整数。设计一个算法来从 中找到最短路径。算法的运行时间应该比 Dijkstra 的运行时间渐进快。
我知道 Dijkstra 的运行时间是 O( e + v log v),并尝试找到更快的算法。
如果所有的权重都是 1 或者只包含 0 和 1,我可以在有向图中使用 BFS O(e+v),但是如何为边权重制定更快的算法是 1 到 20 之间的整数。
我有这个方法签名,我想用EasyMock模拟
public BigDecimal getRemainingPremium(BigDecimal baseAmount, Date commencementDate, Date effectiveDate, boolean isComplete)
Run Code Online (Sandbox Code Playgroud)
我的测试代码有
Premium premium = createMock(Premium.class);
// add this line
EasyMock.expect(premium.getCommencementDate()).andReturn(EasyMock.anyObject(Date.class)).anyTimes();
expect(
premium.getRemainingPremium(
EasyMock.anyObject(BigDecimal.class),
EasyMock.anyObject(Date.class),
EasyMock.anyObject(Date.class),
EasyMock.anyBoolean()
))
.andReturn(BigDecimal.TEN).anyTimes();
Run Code Online (Sandbox Code Playgroud)
但我一直得到这个匹配器的例外.我已经尝试了所有基元组合和'EasyMock.anyObject(Boolean.class)'.有关解决方法的任何建议吗?
java.lang.IllegalStateException: 4 matchers expected, 5 recorded.
This exception usually occurs when matchers are mixed with raw values when recording a method:
foo(5, eq(6)); // wrong
You need to use no matcher at all or a matcher for every single param:
foo(eq(5), eq(6)); // right
foo(5, 6); // also right
at …Run Code Online (Sandbox Code Playgroud) algorithm ×8
java ×4
c++ ×3
optimization ×2
recursion ×2
arrays ×1
dijkstra ×1
easymock ×1
loops ×1
math ×1
matrix ×1
palindrome ×1
performance ×1
permutation ×1
string ×1