- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试运行 hackerrank 的程序。这是问题:
如果字符串中的所有字符出现相同的次数,Sherlock 就会认为该字符串有效。如果他可以仅删除字符串中 one 索引处的 一个 个字符,并且其余字符将出现相同的次数,那么它也是有效的。给定一个字符串,判断它是否有效。
例如,如果 s="abc",则它是有效字符串,因为频率为 {a:1,b:1,c:1}。 abcc 也是如此,因为我们可以删除一个 c,然后在剩余的字符串中每个字符都有 1 个。但是,如果 s='abccc' ,则该字符串无效,因为我们只能删除 1 次出现的 c 。这将留下 {a:1,b:1,c:2} 的字符频率。
失败的 5 个测试用例之一是:aaaabbcc 应该给出 false,但它给了我 true。aabbc 应该给出 true,但给我的是 false。
但不知何故,我的 5 个测试用例出现了错误:这是以下程序。
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class SherlokValidString
{
boolean is_valid(String s)
{
int count=0;
HashMap<Character, Integer> map = new HashMap<>();
char[] str_arr= s.toCharArray();
for(char c:str_arr)
{
if(map.containsKey(c))
{
map.put(c, map.get(c)+1);
}
else
{
map.put(c,1);
}
}
if (map.size()==1)
{
return true;
}
else {
List ll=new ArrayList<>(map.values());
System.out.println(ll);
Collections.sort(ll);
int first_element=(int)ll.get(0);
for(int i=1;i<(ll.size()-1);i++)
{
//System.out.println((int)ll.get(i)+1);
if (first_element==(int)ll.get(i+1))
{
count=0;
}
else if(first_element!=(int)ll.get(i+1))
{
count++;
}
}
if(count<=1)
{
//System.out.println(count);
return true;
}
else
{
return false;
}
}
}
public static void main(String[] args)
{
SherlokValidString svs = new SherlokValidString();
System.out.println(svs.is_valid("abbccc"));
}
}
它应该返回 false,它给了我 true。请帮忙。谢谢。
最佳答案
使用 Java 8,人们可以完成一项非常优雅的工作:
public static boolean sherlockStr(String s) {
// First, we're going to walk over the characters and count how many
// times each character occurs
Map<Character, Long> charOccurs = s.chars()
.mapToObj(t -> (char) t)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
// Then we're going to map each amount of characters found to its count,
// e.g. in the string "aabbcc" each character occurs twice → [2, 2, 2].
// That will yield a map: [2=3]
Map<Long, Long> counts = charOccurs.entrySet().stream()
.map(Map.Entry::getValue)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
switch (counts.size()) {
// If all characters occur an equal number of times, then the map
// contains just a single entry.
case 1:
return true;
// If there are two different amounts, then the difference between
// those two must be 1. Also, one of the numbers must occur exactly
// once.
case 2:
Iterator<Long> it = counts.keySet().iterator();
boolean diff = Math.abs(it.next() - it.next()) == 1;
return (diff && (counts.values().stream()
.anyMatch(i -> i == 1)));
// If the map's size is 3 or more, there are differents of amounts of
// characters.
default:
return false;
}
}
简而言之:
public static boolean sherlockStr(String s) {
Map<Long, Long> counts = s.chars()
.mapToObj(t -> (char) t)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().stream()
.map(Map.Entry::getValue)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
switch (counts.size()) {
case 1:
return true;
case 2:
Iterator<Long> it = counts.keySet().iterator();
return (Math.abs(it.next() - it.next()) == 1 && (counts.values().stream()
.anyMatch(i -> i == 1)));
default:
return false;
}
}
关于java - Sherlock 和有效字符串,在 5 个测试用例中给了我错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51569165/
我正在使用自定义主题,代码如下:- 自定义样式.xml #FFFFFF 40dp @style/CustomWindowTitleBackground
有没有办法将进度对话框添加到 sherlock fragment 的操作栏 sherlock(不是 fragment Activity )。我必须使用 sherlock fragments 因为在我的
我 100% 地受益于 SherlockActionBar。 不过,SherlockFragments 有什么好处?您有主 Activity/FragmentActivity,它托管所有其他 frag
我对 SherlockFragmentActivity 中的 onOptionsItemSelected() 有疑问。onCreateOptionsMenu() 被调用并且选项菜单构建完美,但是当我点
只是尝试在使用 IntelliJ 12 的 Android 应用程序中使用 ActionBar Sherlock 的最简单方法。 我在其他帖子和博客中读过,人们一直在下载 ABS 的源代码并对其进行
我正在尝试向操作栏 sherlock 添加选项卡,但它不断崩溃并且无法启动。我应该在 xml 文件中放入什么,我找不到要放入的内容 MainActivity.java import com.actio
我有一个首选项菜单,我希望它与应用程序保持一致,因此顶部必须有一个操作栏,就像其他 Activity 一样。 我尝试过这样的 public class Preferencemenu extends P
我正在开发一个使用 Android 版操作栏 Sherlock 的应用程序。我目前有三个选项卡,其中包含以下类别的家庭计算器和驱动器。 我之前制作了一个计算器应用程序,我希望能够在计算器选项卡中使用该
我正在尝试实现 Sherlock 操作栏,但我无法在 list 中设置正确的主题,因为它根本不存在。教程说放 android:theme="@style/Theme.Sherlock"但我没有这个选择
我正在使用 Sherlock actionbar 来防止 action bar 的兼容性问题,但我的问题在 android 3.0 或更高版本上不能很好地工作,这是我的屏幕快照.. 在安卓 2.2 中
我设法使用 Sherlock ActionBar 设置了三个选项卡。唯一的问题是当方向改变时,无法再点击选项卡。似乎没有调用 onTabSelected() 。示例:我处于纵向模式并且选择了 tab2
这个问题我遇到过好几次了,每次都自己解决。我不知道我为什么会遇到这个问题。 上次,我只是清理了工作区(之后是 Build Auto)并尝试运行 android 应用程序。 这里是触发错误的代码。
我有 2 个 gradle.build 脚本,一个用于我的应用程序,一个用于我的测试。该应用程序对 actionbarsherlock 的依赖如下: dependencies { compil
可以将操作栏 Sherlock 与 DeviceDefault 主题(在 >4.2 上)和常规主题(因为它曾经在 在 values-v14/themes.xml 中: 然后在 list 中使用
Action Bar Sherlock 虽然是一个整洁的图书馆,但您可能知道,它有两到三个非常侵入性的元素: 它强制您从 SherlockFragments 和 SherlockActivities
我在 Sherlock 操作栏中呈现自定义 View 时遇到问题。发生的事情是我已经成功地在操作栏中设置了自定义 View ,旨在将标题 TextView 设置在中心,但是当我设置其他菜单项(如刷新)
我正在尝试实现一个下拉列表作为 Android 中操作栏的导航。我可以看到下拉列表和项目,但我无法获得点击事件。 自从我按照 http://developer.android.com/guide/to
我想将操作栏标题、标签和菜单项的字体更改为Roboto。 我想知道这是否可以通过 Styles 完成?因为我无法为这些创建自定义 View ,这是分配自定义字体的最佳方式。 我已经浏览了以下链接 ht
我正在使用 sherlock 操作栏版本 4.1.0 (2012-05-17)。如何更改此屏幕截图中文本颜色的颜色?在实际设备上它几乎不可读 我有这个主题 20dp 这在 AndroidM
我已经使用 actionbar sherlock 实现了新的抽屉,显然 actionbar 保持在相同的状态。 我已经实现了 ActionBarDrawerToggle 接口(interface)来处
我是一名优秀的程序员,十分优秀!