Skip to content

Commit

Permalink
158th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jun 26, 2024
1 parent 6760527 commit 9471e68
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 185 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ on: push
jobs:
ci:
runs-on: ubuntu-latest
environment: ${{ github.ref_name }}
steps:
- uses: actions/checkout@v4
- run: docker compose up --build --detach ci
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
"private": true,
"type": "module",
"scripts": {
"lint": "biome check --apply ./",
"lint": "biome check --write ./",
"check": "tsc --noEmit",
"test": "vitest run",
"bench": "vitest bench --run"
},
"devDependencies": {
"@biomejs/biome": "^1.7.3",
"@biomejs/biome": "^1.8.2",
"flatted": "^3.3.1",
"typescript": "^5.4.5",
"vite": "^5.2.11",
"typescript": "^5.5.2",
"vite": "^5.3.1",
"vitest": "^1.6.0"
}
}
310 changes: 155 additions & 155 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

10 changes: 0 additions & 10 deletions src/page-1/35. Search Insert Position/search-insert.test.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/page-1/35. Search Insert Position/search-insert.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/page-1/35. Search Insert Position/searchInsert.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { searchInsert, searchInsert2 } from './searchInsert';

describe('35. Search Insert Position', () => {
test('searchInsert', () => {
expect(searchInsert([1, 3, 5, 6], 5)).toBe(2);
expect(searchInsert([1, 3, 5, 6], 2)).toBe(1);
expect(searchInsert([1, 3, 5, 6], 7)).toBe(4);
});

test('searchInsert2', () => {
expect(searchInsert2([1, 3, 5, 6], 5)).toBe(2);
expect(searchInsert2([1, 3, 5, 6], 2)).toBe(1);
expect(searchInsert2([1, 3, 5, 6], 7)).toBe(4);
});
});
36 changes: 36 additions & 0 deletions src/page-1/35. Search Insert Position/searchInsert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
type SearchInsert = (nums: number[], target: number) => number;

/**
* Accepted
*/
export const searchInsert: SearchInsert = (nums, target) => {
let index = 0;

while (index < nums.length && nums[index] <= target) {
if (nums[index] === target) return index;
index += 1;
}

return index;
};

/**
* Accepted
*/
export const searchInsert2: SearchInsert = (nums, target) => {
let left = 0;
let right = nums.length - 1;

while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (nums[mid] === target) return mid;

if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}

return left;
};
11 changes: 11 additions & 0 deletions src/page-1/8. String to Integer (atoi)/myAtoi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { myAtoi } from './myAtoi';

describe('8. String to Integer (atoi)', () => {
test('myAtoi', () => {
expect(myAtoi('42')).toBe(42);
expect(myAtoi(' -042')).toBe(-42);
expect(myAtoi('1337c0d3')).toBe(1337);
expect(myAtoi('0-1')).toBe(0);
expect(myAtoi('words and 987')).toBe(0);
});
});
37 changes: 37 additions & 0 deletions src/page-1/8. String to Integer (atoi)/myAtoi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export function myAtoi(s: string): number {
const INT_MAX = 2 ** 31 - 1;
const INT_MIN = -(2 ** 31);

let i = 0;
let sign = 1;
let result = 0;

// Ignore leading whitespaces
while (i < s.length && s[i] === ' ') {
i += 1;
}

// Check for sign
if (i < s.length && (s[i] === '-' || s[i] === '+')) {
sign = s[i] === '-' ? -1 : 1;
i += 1;
}

// Convert digits to integer
while (i < s.length && s[i] >= '0' && s[i] <= '9') {
const digit = s.charCodeAt(i) - '0'.charCodeAt(0);

// Handle overflow and underflow
if (
result > Math.floor(INT_MAX / 10) ||
(result === Math.floor(INT_MAX / 10) && digit > INT_MAX % 10)
) {
return sign === 1 ? INT_MAX : INT_MIN;
}

result = result * 10 + digit;
i += 1;
}

return result * sign;
}

0 comments on commit 9471e68

Please sign in to comment.