- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
下面是执行“就地”字符串反转的方法,即黑猫变成猫黑。 在第二个交换部分中,如果使用传统交换(已注释掉),则所有测试都会通过,但如果使用异或交换,则只有一个测试通过。
难道不能简单地“交换”吗
for (int i = count; i <= (end + count) / 2; i++) {
char temp = arr[i];
arr[i] = arr[end - (i - count)];
arr[end - (i - count)] = temp;
}
至
for (int i = count; i <= (end + count) / 2; i++) {
arr[i] ^= arr[end - (i - count)];
arr[end - (i - count)] ^= arr[i];
arr[i] ^= arr[end - (i - count)];
}
方法
public class ReverseString {
public static char[] revString(String input) {
char[] arr = input.toCharArray();
int length = arr.length;
for (int i = 0; i < (length / 2); i++) {
arr[i] ^= arr[length - i - 1];
arr[length - i - 1] ^= arr[i];
arr[i] ^= arr[length - i - 1];
}
int end;
int charCount;
int count = 0;
while (count < length) {
if (arr[count] != ' ') {
charCount = 0;
while (count + charCount < length && arr[count + charCount] != ' ') {
charCount++;
}
end = count + charCount - 1;
// for (int i = count; i <= (end + count) / 2; i++) {
// char temp = arr[i];
// arr[i] = arr[end - (i - count)];
// arr[end - (i - count)] = temp;
// }
for (int i = count; i <= (end + count) / 2; i++) {
arr[i] ^= arr[end - (i - count)];
arr[end - (i - count)] ^= arr[i];
arr[i] ^= arr[end - (i - count)];
}
count += charCount;
} else {
count++;
}
}
return arr;
}
}
测试
@RunWith(JUnitParamsRunner.class)
public class ReverseStringTest {
@Test
@Parameters(method = "getStrings")
public void testRevString(String testValue, char[] expectedValue) {
assertThat(ReverseString.revString(testValue), equalTo(expectedValue));
}
private static final Object[] getStrings() {
return new Object[] {
new Object[] {"Black Cat", "Cat Black".toCharArray()},
new Object[] {"left to", "to left".toCharArray()}
};
}
}
失败的输出
java.lang.AssertionError:
Expected: ["C", "a", "t", " ", "B", "l", "a", "c", "k"]
but: was ["C", "
最佳答案
与自身交换值时,异或交换失败。这是您的代码:
arr[i] ^= arr[end - (i - count)];
arr[end - (i - count)] ^= arr[i];
arr[i] ^= arr[end - (i - count)];
假设i == end - (i - count)
。然后:
arr[i] ^= arr[end - (i - count)];
将arr[i]
设置为零(因为与自身进行异或的任何内容都是零)。
接下来的两行不执行任何操作,因为与零进行异或运算无效,将 arr[i]
保留为零,从而损坏您的输入。
正如您所指出的,上述假设是否成立取决于输入的长度。
由于这个陷阱,异或交换是危险的。由于它也很难阅读,并且在任何现代平台上都无法提高性能,因此这种微优化技巧已经过时,应该避免。
关于java - XOR 交换在所有情况下都等同于传统交换吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25354468/
我是 Redis 的新手,我现在正在尝试制作一个登录功能来学习它。 假设我在传统 SQL 数据库中有一个名为 User (id, username, password) 的表,设计 Redis 的正确
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题?通过 editing this post 添加详细信息并澄清问题. 5年前关闭。 Improve this
我刚刚遇到了具有所有天赐优势的 NoSQL 系统。其中之一似乎是毫不费力的水平缩放。我的问题是,为什么像 MySQL 或 SQL Server 这样的经典 RDBMS 不能进行水平扩展?或者无法做到与
问题 Although you can explicitly check if a value is true or false, it's a convention in JavaScript to
用于突发错误纠正的 ECC 如何工作? “突发错误检测”是指一种技术,可以检测(例如)任何一个 [或两个] 64 位连续位序列中的任何位错误组合。 我需要概念上的解释,而不是数学。 我研究了几种用无穷
我试图向学生说明传统 IO 和 java 中的内存映射文件之间的性能差异。我在互联网上的某个地方找到了一个例子,但我并不是很清楚所有的事情,我什至不认为所有的步骤都是必要的。我在这里和那里阅读了很多关
如何在 PhpStorm 中自动将短数组语法转换为长数组(传统)? 我使用了PhpStorm中的“代码->检查代码”功能,然后一键将短语法转换为长语法。 检查代码 手动转换 必须有一种方法来自动化这项
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我发现 Light Table 是尝试 Clojure 语言的好工具。 它有一个方便的功能叫做 Instarepl - ;;您在此处输入的任何内容都将立即执行,结果显示在右侧。 但是,我找不到使用旧式
我们有一个经典的 ASP 应用程序,所有数据都是使用表格呈现的(是的,我们正在努力将其转换为 DIV,但我们离那还有一段距离)。我们设置了一个标准的表格宽度,通常这可以正常工作,但有些元素的大小可以由
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 年前。 Improve
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 6年前关闭。 Improve this
这只是一个一般性问题。传统的 ajax 和 jquery ajax 哪个更好?因为当我使用 jquery ajax 时,它似乎在 IE 中做了一些奇怪的事情。它应该是一个跨浏览器库......所以我对
所有这些都是为了丢弃MVC Controller 的问题。 这是ajax的代码: $.ajax({ //tipo de transferencia t
我知道它可能应该是另一种方式(OSGI 运行时托管 Java EE 服务器),因为它是 apparently already possible with glassfish. 但是,我们的客户在管理传
作为具有一些 winforms 和客户端应用程序经验的人 - 是否值得回去学习传统的 ASP .NET 页面的工作方式,或者直接进入 ASP .NET MVC 是否可以? 我正在寻找我对 C# 的一般
如果我错了,请纠正我:在传统的Web应用程序中,浏览器会自动将 session 信息附加到对服务器的请求中,以便服务器可以知道该请求来自谁。实际上到底附加了什么? 但是,在基于 API 的应用程序中,
我正在使用Eclipse neon 3,当我导入一个使用WebSphere Application Server传统V9.0作为运行时环境的项目时,它显示了一个错误,宣布 Target runtime
背景: 我的 REST 服务项目是使用 Hibernate 启动的。我在域类中使用 id (Long) 作为 rest url 中标识符的一部分,例如: http://abc.com/customer
在 Scheme 中递归构建列表时,我看到两种类型的示例散布在互联网上。其中一个新值附加了 append每一次迭代。另一个在每次迭代前都添加一个新值 cons然后列表完成后reverse被调用一次。
我是一名优秀的程序员,十分优秀!