XOR链表基本上是链表的有效版本,其存储前一节点和下一节点的地址以仅使用单个指针来实现双链表.我想知道是否有可能在Java中实现,因为它没有指针.在C中,这可以通过
/* Insert a node at the begining of the XORed linked list and makes the
newly inserted node as head */
void insert(struct node **head_ref, int data)
{
// Allocate memory for new node
struct node *new_node = (struct node *) malloc (sizeof (struct node));
new_node->data = data;
/* Since new node is being inserted at the begining, npx of new node
will always be XOR of current head and NULL */
new_node->npx = XOR(*head_ref, NULL);
/* If linked …Run Code Online (Sandbox Code Playgroud) 假设我有一个Pair类
public class Pair<P, Q> {
public P p;
public Q q;
public Pair(P p, Q q) {
this.p = p;
this.q = q;
}
public int firstValue() {
return ((Number)p).intValue();
}
public int secondValue() {
return ((Number)q).intValue();
}
}
Run Code Online (Sandbox Code Playgroud)
我希望对它进行排序,首先是第一个值,然后是第二个值.现在'如果我这样做
List<Pair<Integer, Integer>> pairList = new ArrayList<>();
pairList.add(new Pair<>(1, 5));
pairList.add(new Pair<>(2, 2));
pairList.add(new Pair<>(2, 22));
pairList.add(new Pair<>(1, 22));
pairList.sort(Comparator.comparing(Pair::firstValue));
Run Code Online (Sandbox Code Playgroud)
一切都运行良好,列表按对的第一个值排序,但如果我这样做
pairList.sort(Comparator.comparing(Pair::firstValue).thenComparing(Pair::secondValue));
Run Code Online (Sandbox Code Playgroud)
它失败了,错误
Error:(24, 38) java: incompatible types: cannot infer type-variable(s) T,U
(argument mismatch; invalid method reference
method firstValue in …Run Code Online (Sandbox Code Playgroud) 我试图解决这个问题几个星期,但无法达成解决方案.首先,两个数字X和Y都等于1.只有有效的选项是X+Y或者Y+X一次.我们需要找到需要达到特定数量的最小迭代次数.
例如:如果数字是5
X=1, Y=1; X = X+Y
X=2, Y=1; Y = X+Y
X=2, Y=3; Y = Y+X
X=2, Y=5; Stop answer reached
Run Code Online (Sandbox Code Playgroud)
我的看法:如果一个数字是奇数,那就说23,递减1.现在值= 22.找到除以22 = 11的最大数字.现在通过加1来达到数字,这样:
X=11; Y=1 ; Y=Y+X
X=11; Y=12; X=X+Y
X=23, answer reached
Run Code Online (Sandbox Code Playgroud)
但是这种方法的问题是我不能递归地达到一个特定的数字,因为即使我达到某一点,比如X =所需的值,Y值也会被错放,我无法重复使用它来达到另一个值
我有一个基于 LLVM 的 clang 格式,但我发现很难找到一个在评论开始后添加空间的设置。基本上,我想要:
// This is a comment
Run Code Online (Sandbox Code Playgroud)
代替:
//This is a comment
Run Code Online (Sandbox Code Playgroud)
这是.clang-format我目前正在使用的:
# Generated from CLion C/C++ Code Style settings
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: Consecutive
AlignOperands: true
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Always
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Custom
AlignTrailingComments: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterUnion: false
BeforeCatch: false
BeforeElse: …Run Code Online (Sandbox Code Playgroud) 我已经制作了自己的Tree类,并且我试图检查两棵树是否相同.但这里的问题是我正在使用这个电话:
Tree myTree = new Tree();
Tree mySecondTree = new Tree();
myTree.isIdentical(myTree, mySecondTree);
Run Code Online (Sandbox Code Playgroud)
以这种方式传递它有点奇怪,我想通过这种方式传递它:
myTree.isIdentical(mySecondTree);
Run Code Online (Sandbox Code Playgroud)
isIdentical function :
class Tree<T>{
T data;
Tree left;
Tree right;
Tree(T data){
this.data = data;
}
public boolean isIdentical(Tree t1, Tree t2){
if(t1 == t2)
return true;
if(t1==null || t2==null)
return false;
return (
(t1.data == t2.data) &&
(isIdentical(t1.left, t2.left)) &&
(isIdentical(t1.right, t2.right))
);
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用Stack,但我有点坚持这个
我正在使用埃拉托色尼的 Seive 来计算第 1,000,001 个素数,但是,我无法计算使用 Seive 的上限。我的功能:
public static void Seive(int num){
BitSet primes = new BitSet();
for(int i=2; i<=num; i++){
if(!primes.get(i)){
for(int j=i+i; j<=num; j+=i){
primes.set(j);
}
}
}
for(int i=2; i<=num; i++){
if(!primes.get(i))
System.out.print(i + " ");
}
}
Run Code Online (Sandbox Code Playgroud)
计算从 2 到 num 的素数,但如果我不知道范围但有兴趣找到第 n 个数字怎么办。
今天在HackerRank上解决这个问题时,我使用了Array stream .sum()函数对所有条目求和并继续我的算法.但总而言之,我发现我的算法在某些情况下失败了.我使用diff来发现它通过了99%的情况,1%的输出几乎相等,但小于原始答案.这就是为什么我用for循环替换了流.sum(),并且意外地它通过了所有的测试用例.我试过但无法确定这种不确定的行为.
我使用stream.sum()的实现:
public class MandragoraForest {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
for (int i = in.nextInt(); i > 0; i--) {
int number = in.nextInt();
int[] h = new int[number];
for (int j = 0; j < number; j++) h[j] = in.nextInt();
System.out.println(new MandragoraForestSolver().solve(h));
}
}
}
class MandragoraForestSolver {
public long solve(int[] h) {
if (h.length==1) return h[0];
Arrays.parallelSort(h);
long sum = Arrays.stream(h)
.sum();
long ans = -1;
for …Run Code Online (Sandbox Code Playgroud)