Algorithm
描述: 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
思路: 使用一个新头结点来作为起始,然后对比两个链接即可。较常见是3个连续的循环。写的时候发现也可以对循环进行嵌套:
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
new_head = ListNode(-1)
cur = new_head
while l1 or l2:
while l1:
if l2 and l1.val > l2.val:
break
cur.next = l1
cur, l1 = cur.next, l1.next
while l2:
if l1 and l2.val > l1.val:
break
cur.next = l2
cur, l2 = cur.next, l2.next
return new_head.next
Review
Android: CircleImageView Simplified
前言:如何正确选择第三方库
- 选择一直有维护的库。在 GitHub 的 Insights -> Contributors 里能看到代码的更新频率。
- 选择有很多贡献者的库。同样在 Contributors 页可以看到。
- 选择由谷歌、Square等知名公司开发的库。
1. 原生实现 CircleImageView
通过 shape 可以很容易实现圆形图片。用法详见 创建阴影和裁剪视图 。
<!--bg_circle.xml-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@android:color/darker_gray"/>
</shape>
- 创建圆形的 shape 。代码如上。
- 在 xml 中使用
android:background=”@drawable/bg_circle”或在代码中使用imageView.setBackgroundResource(R.drawable.bg_circle)来设置圆形。 - 使用
imageView.setClipToOutline(true)裁剪图片(xml 中不可用)。
最终效果如下:
2. 使用 Material Design Components (MDC) 库实现 CircleImageView
如果已经使用了 1.2.0 及以上版本的 MDC 库,可以使用 ShapeableImageView ,通过设置 shapeApperance 属性,可以实现很多有趣的造型。
实现圆形的方法如下:
<!--styles.xml-->
<style name="ShapeAppearance.App.CircleImageView" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="centerCrop"
app:srcCompat="@drawable/ironman"
app:shapeAppearance="@style/ShapeAppearance.App.CircleImageView"/>
3. 使用 Jetpack Compose 实现 CircleImageView
Jetpack Compose 令绘制 UI 更加容易,也不需要修改 xml 。实现圆形的方法如下:
Image(
imageResource(R.drawable.ironman),
modifier = Modifier
.height(200.dp)
.width(200.dp)
.clip(CircleShape),
contentScale = ContentScale.Crop
)
Tip
pospec 里 preserve_paths 是用来保留下载后的文件,匹配的的文件在下载后会保留。
preserve_paths 配置出错可能导致 cache 里缓存的文件出错。
Share
无