Skip to content

Instantly share code, notes, and snippets.

View carolinemusyoka's full-sized avatar
:octocat:
so?.let { it -> be (it) }

Carol carolinemusyoka

:octocat:
so?.let { it -> be (it) }
View GitHub Profile
@carolinemusyoka
carolinemusyoka / TwoSum.kt
Created October 28, 2021 12:16
1. Two Sum
//Solves the problem in O(n) time complexity
class Solution {
fun twoSum(nums: IntArray, target: Int): IntArray {
//initialize an empty HashMap
val map = hashMapOf<Int, Int>()
//and for every element in the array, if it exists in the Map,
//check if its complement exists a in the Map or not. If the complement exists then return the indices
//of the current element and the complement.
//Otherwise, put the element in the Map, and move to the next iteration.
for(i in 0..nums.size - 1){
/**
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
class Solution {
//Finding the length of the longest substring without repeating characters
//Solution
// Iterate through an array, evaluating if new substring should be the max substring
//Time complexity is O(n)..since we're iterating through an array once
//Space complexity is constant
class Solution {
fun lengthOfLongestSubstring(str: String): Int{
val map: HashMap<Char, Int> = hashMapOf()
var max = 0
// last repeat of any character
class Solution {
fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double {
// Check if num1 is smaller than num2
// If not, then we will swap num1 with num2
if (nums1.size > nums2.size) {
return findMedianSortedArrays(nums2, nums1)
}
// Lengths of two arrays
val m = nums1.size
val n = nums2.size
class Solution {
fun reverse(x: Int): Int {
var res = 0
var n = x
while (n != 0) {
if (Math.abs(res) > Int.MAX_VALUE/10) return 0
res = res*10 + n % 10
n /= 10
}
val builder = LatLngBounds.Builder()
val locBounds = LatLngBounds(LatLng(53.394655, 10.09989), LatLng(53.694865, 9.75758))
builder.include(locBounds.southwest)
builder.include(locBounds.northeast)
googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100))
val regionId = MutableLiveData<Int>()
fun setRegionId(id: Int){
regionId.value = id
}
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="45"
android:startColor="@color/green"
android:centerColor="@color/white"
android:endColor="@color/orange" />
</shape>
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val parsedDate = LocalDateTime.parse(item.created_at, DateTimeFormatter.ISO_DATE_TIME)
val formattedDate = parsedDate.format(DateTimeFormatter.ofPattern("dd.MM.yyyy | HH:mm"))
textCreatedTime.text = formattedDate
} else {
val parser = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
val formatter = SimpleDateFormat("dd.MM.yyyy | HH:mm")
val formattedDate = formatter.format(parser.parse(item.created_at)!!)
textCreatedTime.text = formattedDate
}
@carolinemusyoka
carolinemusyoka / UploadImageWithMultiPart.kt
Created April 22, 2022 17:23
Uploading an image using multipart in Kotlin
// 1. The Interface
// --> Two ways to do it,
```
@Multipart
@POST("the_endpoint")
suspend fun postImage(
@Part profile_photo: MultipartBody.Part,
@Part ("profile_photo") requestBody: RequestBody