This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def lastStoneWeight(self, stones: List[int]) -> int: | |
| sorted_stones = list(sorted(stones)) | |
| while True: | |
| if len(sorted_stones) > 1: | |
| x = sorted_stones[-2] | |
| y = sorted_stones[-1] | |
| del sorted_stones[-1] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def merge(self, intervals): | |
| out = [] | |
| for i in sorted(intervals, key=lambda i: i.start): | |
| if out and i.start <= out[-1].end: | |
| out[-1].end = max(out[-1].end, i.end) | |
| else: | |
| out += i, | |
| return out |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def canJump(self, nums: List[int]) -> bool: | |
| last_position = len(nums)-1 | |
| for i in range(len(nums)-2,-1,-1): item | |
| if (i + nums[i]) >= last_position:the last position | |
| last_position = i | |
| return last_position == 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| vector<int> plusOne(vector<int>& digits) { | |
| bool next_step = true; | |
| int pos = digits.size() - 1; | |
| while (next_step && (pos >= 0)) { | |
| digits[pos] += 1; | |
| if (digits[pos] == 10) { | |
| digits[pos] = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int lengthOfLastWord(string s) { | |
| int len = 0, tail = s.length() - 1; | |
| while (tail >= 0 && s[tail] == ' ') tail--; | |
| while (tail >= 0 && s[tail] != ' ') { | |
| len++; | |
| tail--; | |
| } | |
| return len; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| ListNode* swapPairs(ListNode* head) { | |
| ListNode **pp = &head, *a, *b; | |
| while ((a = *pp) && (b = a->next)) { | |
| a->next = b->next; | |
| b->next = a; | |
| *pp = b; | |
| pp = &(a->next); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| def longestPalindrome(self, s: str) -> str: | |
| res = "" | |
| for i in range(len(s)): | |
| res = max(self.helper(s, i, i), self.helper(s, i, i+1), res, key=len) | |
| return res | |
| def helper(self, s, l, r): | |
| while 0 <= l and r < len(s) and s[l] == s [r]: | |
| l -= 1; r += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func Search(n int, f func(int) bool) int { | |
| // Define f(-1) == false and f(n) == true. | |
| // Invariant: f(i-1) == false, f(j) == true. | |
| i, j := 0, n | |
| for i < j { | |
| h := i + (j-i)/2 // avoid overflow when computing h | |
| // i ≤ h < j | |
| if !f(h) { | |
| i = h + 1 // preserves f(i-1) == false | |
| } else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func main() { | |
| a := []int{1, 3, 6, 10, 15, 21, 28, 36, 45, 55} | |
| x := 6 | |
| i := sort.Search(len(a), func(i int) bool { return a[i] >= x }) | |
| if i < len(a) && a[i] == x { | |
| fmt.Printf("found %d at index %d in %v\n", x, i, a) | |
| } else { | |
| fmt.Printf("%d not found in %v\n", x, a) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class obj { | |
| int v; | |
| public: | |
| obj(int value) : v(value) { } | |
| void swap(obj& other) { | |
| using swap; | |
| swap(this->w, other.v); | |
| } | |
| }; |
NewerOlder