这是一个在求职面试中出现的问题.我们的想法是定义一个数据结构,而不是使用Java内置的LinkedHashMap.
LRU高速缓存删除最近最少使用的条目以插入新条目.因此,给出以下场景:
A - B - C - D - E
Run Code Online (Sandbox Code Playgroud)
如果A是最近最少使用的项目,如果我们要插入F,我们需要删除A.
如果我们通过(键,值)保存带有缓存条目的HashMap以及包含元素的键和使用时间的单独列表,则可以轻松实现这一点.但是,我们需要查询列表以找到最近最少使用的项目,具有潜在的O(n)时间复杂度.
如何在Java中为通用对象和O(1)操作实现此结构?
这与可能的重复不同,因为它侧重于效率(O(1)ops)和实现数据结构本身,而不是扩展Java.
我正在尝试定义一个布局,其中a ImageView在屏幕底部对齐,也在a下面GridView,以避免重叠.
然而,这导致ImageView被设置GridView在屏幕下方但未与屏幕底部对齐.
这可以使用不同类型的布局来解决吗?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/background"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/gs_top_text_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingBottom="30dp">
<RelativeLayout
android:id="@+id/gs_top_sub_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:paddingBottom="30dp">
<TextView
android:id="@+id/text_l"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<TextView
android:id="@+id/text_t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/gs_top_sub_container"
android:layout_centerHorizontal="true">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text1"/>
</RelativeLayout>
</RelativeLayout>
<GridView
android:id="@+id/gs_grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/gs_top_text_container"
android:descendantFocusability="blocksDescendants"
android:horizontalSpacing="2dp"
android:verticalSpacing="2dp"
android:numColumns="3"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp" />
<ImageView
android:id="@+id/gs_bottom_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content" …Run Code Online (Sandbox Code Playgroud) 我cat | zgrep在远程服务器上运行几个命令并单独收集它们的输出以进行进一步处理:
class MainProcessor(mp.Process):
def __init__(self, peaks_array):
super(MainProcessor, self).__init__()
self.peaks_array = peaks_array
def run(self):
for peak_arr in self.peaks_array:
peak_processor = PeakProcessor(peak_arr)
peak_processor.start()
class PeakProcessor(mp.Process):
def __init__(self, peak_arr):
super(PeakProcessor, self).__init__()
self.peak_arr = peak_arr
def run(self):
command = 'ssh remote_host cat files_to_process | zgrep --mmap "regex" '
log_lines = (subprocess.check_output(command, shell=True)).split('\n')
process_data(log_lines)
Run Code Online (Sandbox Code Playgroud)
但是,这会导致子进程('ssh ... cat ...')命令的顺序执行.第二个峰值等待第一个完成,依此类推.
如何修改此代码以便子进程调用并行运行,同时仍能够单独收集每个的输出?
我有一个巨大的MongoDB数据库,由mongoengine驱动,其中的对象有一个日期.为了使工作更轻松,我想存档旧对象,但将它们保存在某处.
我一直在阅读文档,并碰上了switch_db和switch_collection.但是,我不能做任何工作.
对于这两种情况,文档都引用了两种使用方案.
作为一项QuerySet行动:
user = User.objects.get(id=user_id)
user.switch_collection('old-users')
user.save()
Run Code Online (Sandbox Code Playgroud)
这个问题是它只适用于单个对象.无法批量存档多个文档.
作为context_manager:
with switch_collection(Group, 'group1') as Group:
Group(name="hello testdb!").save() # Saves in group1 collection
Run Code Online (Sandbox Code Playgroud)
使用此我甚至无法进行查询,收到以下错误:
ValidationError (Document:None) (Field is required...
Run Code Online (Sandbox Code Playgroud)我已经尝试使用mongoengine搜索存档数据的方法,但似乎没有一个选项可行.你有什么建议吗?
从API 17开始,可以获得手机的实际屏幕尺寸:
if (android.os.Build.VERSION.SDK_INT >= 17){
display.getRealSize(size);
int screen_width = size.x;
screen_height = size.y;
} else {...}
Run Code Online (Sandbox Code Playgroud)
我想获得API 8-16的真实屏幕尺寸.在这种情况下处理else条件的最佳方法是什么?
android ×2
python ×2
alignment ×1
generics ×1
java ×1
layout ×1
mongoengine ×1
screen ×1
shell ×1
subprocess ×1