Top100(上)
散列
| struct MyListNode { |
| int val; |
| int pos; |
| struct MyListNode *next; |
| }; |
| |
| |
| typedef struct { |
| struct MyListNode *data; |
| } MyHashMap; |
| |
| const int hashSize = 1543; |
| |
| MyHashMap *createHashMap() { |
| MyHashMap *hashMap = (MyHashMap *) malloc(sizeof(MyHashMap)); |
| hashMap->data = (struct MyListNode *) malloc(sizeof(struct MyListNode) * hashSize); |
| for (int i = 0; i < hashSize; ++i) { |
| hashMap->data[i].val = 0x80000000; |
| hashMap->data[i].pos = -1; |
| hashMap->data[i].next = NULL; |
| } |
| return hashMap; |
| } |
| |
| int hash(int key) { |
| return (key+1000000000) % hashSize; |
| } |
| |
| struct MyListNode *getList(MyHashMap *hashMap, int key) { |
| return &(hashMap->data[hash(key)]); |
| } |
| |
| int containsKey(MyHashMap *hashMap, int key) { |
| struct MyListNode *head = getList(hashMap, key); |
| while (head != NULL) { |
| if (head->val == key) return head->pos; |
| head = head->next; |
| } |
| return -1; |
| } |
| |
| void insert(MyHashMap *hashMap, int key, int pos) { |
| if (containsKey(hashMap, key) != -1)return; |
| struct MyListNode *head = getList(hashMap, key); |
| struct MyListNode *node = (struct MyListNode *) malloc(sizeof(struct MyListNode)); |
| node->val = key; |
| node->pos = pos; |
| node->next = head->next; |
| head->next = node; |
| } |
| |
| |
| int *twoSum(int *nums, int numsSize, int target, int *returnSize) { |
| int *res = (int *) malloc(sizeof(int) * 2); |
| *returnSize = 0; |
| MyHashMap *hashMap = (MyHashMap *) malloc(sizeof(MyHashMap)); |
| hashMap = createHashMap(); |
| |
| for (int i = 0; i < numsSize; ++i) { |
| int t = containsKey(hashMap, target - nums[i]); |
| if (t != -1) { |
| res[0] = i; |
| res[1] = t; |
| *returnSize = 2; |
| return res; |
| } else { |
| insert(hashMap, nums[i], i); |
| } |
| } |
| return res; |
| } |
| |
| |
| class Solution { |
| public List<List<String>> groupAnagrams(String[] strs) { |
| Map<String, List<String>> map = new HashMap<String, List<String>>(); |
| for (String str : strs) { |
| |
| char[] chars = str.toCharArray(); |
| |
| Arrays.sort(chars); |
| String key = new String(chars); |
| List<String> list = map.getOrDefault(key, new ArrayList<String>()); |
| |
| list.add(str); |
| map.put(key, list); |
| } |
| return new ArrayList<List<String>>(map.values()); |
| } |
| } |
| |
| class Solution { |
| public List<List<String>> groupAnagrams(String[] strs) { |
| Map<String, List<String>> map = new HashMap<String, List<String>>(); |
| for (String str : strs) { |
| int[] counts = new int[26]; |
| int length = str.length(); |
| |
| for (int i = 0; i < length; i++) { |
| counts[str.charAt(i) - 'a']++; |
| } |
| StringBuilder sb = new StringBuilder(); |
| |
| for (int i = 0; i < 26; i++) { |
| if (counts[i] != 0) { |
| sb.append((char) ('a' + i)); |
| sb.append(counts[i]); |
| } |
| } |
| String key = sb.toString(); |
| List<String> list = map.getOrDefault(key, new ArrayList<String>()); |
| list.add(str); |
| map.put(key, list); |
| } |
| return new ArrayList<List<String>>(map.values()); |
| } |
| } |
| struct Node { |
| char *value; |
| struct Node *next; |
| }; |
| |
| struct DummyNode { |
| |
| char *sortedStr; |
| |
| int count; |
| struct Node *next; |
| }; |
| |
| typedef struct { |
| struct DummyNode *data; |
| |
| int groupCount; |
| int hashSize; |
| } HashMap; |
| |
| int cmp(const void *a, const void *b) { |
| return *(char *) a - *(char *) b; |
| } |
| |
| HashMap *createHashMap(int strsSize) { |
| HashMap *hashMap = (HashMap *) malloc(sizeof(HashMap)); |
| |
| hashMap->hashSize = strsSize; |
| hashMap->groupCount = 0; |
| |
| hashMap->data = (struct DummyNode *) malloc(sizeof(struct DummyNode) * strsSize); |
| |
| for (int i = 0; i < strsSize; ++i) { |
| hashMap->data[i].sortedStr = ""; |
| hashMap->data[i].count = 0; |
| hashMap->data[i].next = NULL; |
| } |
| return hashMap; |
| } |
| |
| |
| struct DummyNode *findNodeList(HashMap *hashMap, char *sorted) { |
| |
| for (int i = 0; i < hashMap->groupCount; ++i) |
| |
| if (strcmp(hashMap->data[i].sortedStr, sorted) == 0) |
| return &(hashMap->data[i]); |
| |
| hashMap->data[hashMap->groupCount].sortedStr = (char *) malloc(sizeof(char) * (strlen(sorted) + 1)); |
| strcpy(hashMap->data[hashMap->groupCount].sortedStr, sorted); |
| |
| hashMap->groupCount++; |
| return &(hashMap->data[hashMap->groupCount - 1]); |
| } |
| |
| void addToHashMap(HashMap *hashMap, char *str) { |
| |
| char buff[strlen(str) + 1]; |
| strcpy(buff, str); |
| |
| qsort(buff, strlen(buff), sizeof(char), cmp); |
| |
| struct DummyNode *dummyHead = findNodeList(hashMap, buff); |
| struct Node *node = (struct Node *) malloc(sizeof(struct Node)); |
| node->value = str; |
| node->next = dummyHead->next; |
| dummyHead->next = node; |
| |
| dummyHead->count++; |
| } |
| |
| |
| char ***groupAnagrams(char **strs, int strsSize, int *returnSize, int **returnColumnSizes) { |
| *returnSize = 0; |
| if (strsSize <= 0 || strs == NULL) return NULL; |
| HashMap *hashMap = createHashMap(strsSize); |
| |
| for (int i = 0; i < strsSize; ++i) |
| addToHashMap(hashMap, strs[i]); |
| |
| int groupCount = hashMap->groupCount; |
| *returnSize = groupCount; |
| char ***res = (char ***) malloc(sizeof(char **) * groupCount); |
| *returnColumnSizes = (int *) calloc(groupCount, sizeof(int)); |
| for (int i = 0; i < groupCount; ++i) { |
| int len = hashMap->data[i].count; |
| |
| (*returnColumnSizes)[i] = len; |
| res[i] = (char **) malloc(sizeof(char *) * len); |
| struct Node *cur = hashMap->data[i].next; |
| for (int j = 0; j < len; ++j) { |
| res[i][j] = cur->value; |
| cur = cur->next; |
| } |
| } |
| |
| return res; |
| } |
| int cmp(const void *a, const void *b) { |
| return (*(int *) a) - (*(int *) b); |
| } |
| |
| |
| int longestConsecutive(int *nums, int numsSize) { |
| qsort(nums, numsSize, sizeof(int), cmp); |
| int maxLen = 0, tempLen = 0; |
| for (int i = 0; i < numsSize; ++i) { |
| if (i == 0) { |
| tempLen++; |
| maxLen = 1; |
| } else { |
| if (nums[i] - nums[i - 1] == 0) continue; |
| if (nums[i] - nums[i - 1] == 1) { |
| tempLen++; |
| if (tempLen > maxLen) maxLen = tempLen; |
| } else { |
| |
| tempLen = 1; |
| } |
| } |
| } |
| return maxLen; |
| } |
| class Solution { |
| |
| public int longestConsecutive(int[] nums) { |
| Set<Integer> set = new HashSet<>(); |
| |
| for (int num : nums) set.add(num); |
| |
| int maxLen = 0; |
| for (Integer num : set) { |
| |
| if (!set.contains(num - 1)) { |
| |
| int tempLen = 1; |
| int curNum = num + 1; |
| |
| while (set.contains(curNum)) { |
| tempLen++; |
| curNum++; |
| } |
| maxLen = Math.max(tempLen, maxLen); |
| } |
| } |
| return maxLen; |
| } |
| } |
双指针
| void moveZeroes(int *nums, int numsSize) { |
| |
| int slow = 0; |
| |
| int fast = 0; |
| while (fast < numsSize) { |
| if (nums[fast] != 0) { |
| nums[slow++] = nums[fast]; |
| } |
| fast++; |
| } |
| |
| while (slow < numsSize) { |
| nums[slow++] = 0; |
| } |
| } |
| int min(int a, int b) { |
| return a < b ? a : b; |
| } |
| |
| int max(int a, int b) { |
| return a > b ? a : b; |
| } |
| |
| int maxArea(int *height, int heightSize) { |
| int res = 0; |
| for (int i = 0; i < heightSize; ++i) { |
| for (int j = i + 1; j < heightSize; ++j) { |
| int temp = (j - i) * min(height[i], height[j]); |
| res = max(res, temp); |
| } |
| } |
| return res; |
| } |
| |
| int min(int a, int b) { |
| return a < b ? a : b; |
| } |
| |
| int max(int a, int b) { |
| return a > b ? a : b; |
| } |
| |
| int maxArea(int *height, int heightSize) { |
| int left = 0, right = heightSize - 1; |
| int res = 0; |
| while (left < right) { |
| int temp = (right - left) * min(height[left], height[right]); |
| res = max(res, temp); |
| |
| |
| if (height[left] < height[right]) |
| left++; |
| else |
| right--; |
| } |
| return res; |
| } |
| int cmp(const void *a, const void *b) { |
| return (*(int *) a) - (*(int *) b); |
| } |
| |
| |
| const int SIZE = 20000; |
| |
| int **threeSum(int *nums, int numsSize, int *returnSize, int **returnColumnSizes) { |
| if (nums == NULL || numsSize < 3) return NULL; |
| int **res = (int **) malloc(sizeof(int *) * SIZE); |
| int index = 0; |
| *returnColumnSizes = (int *) malloc(sizeof(int) * SIZE); |
| *returnSize = 0; |
| |
| |
| qsort(nums, numsSize, sizeof(int), cmp); |
| |
| |
| if (nums[numsSize - 1] < 0) return NULL; |
| |
| for (int i = 0; i < numsSize - 2; ++i) { |
| |
| if (nums[i] > 0) break; |
| if (i > 0) { |
| |
| |
| while ((i < numsSize - 2) && nums[i] == nums[i - 1]) i++; |
| } |
| int left = i + 1, right = numsSize - 1; |
| while (left < right) { |
| int sum = nums[i] + nums[left] + nums[right]; |
| if (sum == 0) { |
| res[index] = (int *) malloc(sizeof(int) * 3); |
| res[index][0] = nums[i]; |
| res[index][1] = nums[left]; |
| res[index][2] = nums[right]; |
| index++; |
| |
| |
| |
| |
| while (left < right && nums[left] == nums[left + 1]) left++; |
| |
| left++; |
| while (left < right && nums[right] == nums[right - 1]) right--; |
| right--; |
| |
| if (left >= right) break; |
| } else if (sum > 0) { |
| |
| while (left < right && nums[right] == nums[right - 1]) right--; |
| right--; |
| } else { |
| |
| while (left < right && nums[left] == nums[left + 1]) left++; |
| left++; |
| } |
| } |
| } |
| |
| *returnSize = index; |
| *returnColumnSizes = (int *) malloc(sizeof(int) * index); |
| for (int i = 0; i < index; ++i) { |
| (*returnColumnSizes)[i] = 3; |
| } |
| |
| return res; |
| } |
| |
| int trap(int *height, int heightSize) { |
| int res = 0; |
| int lever = 1; |
| |
| int maxHeight = 0; |
| for (int i = 0; i < heightSize; ++i) |
| if (height[i] > maxHeight) maxHeight = height[i]; |
| |
| |
| while (lever <= maxHeight) { |
| int i = 0; |
| |
| while (i < heightSize && height[i] < lever) i++; |
| if (i >= heightSize) continue; |
| int temp = 0; |
| while (i < heightSize) { |
| if (height[i] < lever) { |
| |
| temp++; |
| } else if (height[i] >= lever) { |
| |
| |
| res += temp; |
| temp = 0; |
| } |
| i++; |
| } |
| lever++; |
| } |
| return res; |
| } |
| int findMax(int *height, int start, int end) { |
| int max = height[start]; |
| while (start <= end) { |
| if (height[start] > max) max = height[start]; |
| start++; |
| } |
| return max; |
| } |
| |
| |
| int trap(int *height, int heightSize) { |
| int res = 0; |
| |
| |
| int tempLeftMax = height[0]; |
| int leftMaxArr[heightSize]; |
| for (int i = 1; i <= heightSize - 2; ++i) { |
| leftMaxArr[i] = tempLeftMax; |
| if (height[i] > tempLeftMax) tempLeftMax = height[i]; |
| } |
| |
| |
| int tempRightMax = height[heightSize - 1]; |
| int rightMaxArr[heightSize]; |
| for (int i = heightSize - 2; i >= 1; i--) { |
| rightMaxArr[i] = tempRightMax; |
| if (height[i] > tempRightMax) tempRightMax = height[i]; |
| } |
| |
| for (int i = 1; i < heightSize - 1; ++i) { |
| |
| |
| |
| |
| |
| int leftMax = leftMaxArr[i]; |
| int rightMax = rightMaxArr[i]; |
| int min = leftMax < rightMax ? leftMax : rightMax; |
| |
| |
| if (min > height[i]) res += min - height[i]; |
| } |
| return res; |
| } |
| |
| |
| int trap(int *height, int heightSize) { |
| int res = 0; |
| |
| |
| int tempRightMax = height[heightSize - 1]; |
| int rightMaxArr[heightSize]; |
| for (int i = heightSize - 2; i >= 1; i--) { |
| rightMaxArr[i] = tempRightMax; |
| if (height[i] > tempRightMax) tempRightMax = height[i]; |
| } |
| |
| |
| int leftMax = height[0], rightMax; |
| |
| for (int i = 1; i < heightSize - 1; ++i) { |
| |
| rightMax = rightMaxArr[i]; |
| int min = leftMax < rightMax ? leftMax : rightMax; |
| |
| |
| if (min > height[i]) res += min - height[i]; |
| |
| if (height[i] > leftMax) leftMax = height[i]; |
| } |
| return res; |
| } |
| |
| int trap(int *height, int heightSize) { |
| int res = 0; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| int leftMaxOfL = height[0], rightMaxOfR = height[heightSize - 1]; |
| |
| int L = 1, R = heightSize - 2; |
| |
| for (int i = 1; i < heightSize - 1; ++i) { |
| |
| |
| |
| if (height[L - 1] < height[R + 1]) { |
| |
| |
| |
| |
| int min = leftMaxOfL; |
| if (min > height[L]) res += min - height[L]; |
| |
| if (height[L] > leftMaxOfL) leftMaxOfL = height[L]; |
| L++; |
| } else { |
| |
| int min = rightMaxOfR; |
| if (min > height[R]) res += min - height[R]; |
| |
| if (height[R] > rightMaxOfR) rightMaxOfR = height[R]; |
| R--; |
| } |
| } |
| return res; |
| } |
| |
| int trap(int *height, int heightSize) { |
| int res = 0; |
| int stack[heightSize]; |
| int top = 0; |
| int current = 0; |
| while (current < heightSize) { |
| |
| while (top > 0 && height[current] > height[stack[top - 1]]) { |
| |
| int h = height[stack[top - 1]]; |
| top--; |
| if (top == 0) break; |
| |
| int distance = current - stack[top - 1] - 1; |
| |
| int min = height[stack[top - 1]] < height[current] ? height[stack[top - 1]] : height[current]; |
| res += distance * (min - h); |
| } |
| stack[top++] = current; |
| current++; |
| } |
| return res; |
| } |
滑动窗口
| int lengthOfLongestSubstring(char *s) { |
| int len = strlen(s); |
| if (len < 2) return len; |
| |
| |
| int *hashSet = (int *) calloc(128, sizeof(int)); |
| int left = 0, right = 0; |
| hashSet[s[right]] = 1; |
| int res = 1; |
| |
| while (right + 1 < len) { |
| |
| char nextChar = s[right + 1]; |
| if (hashSet[nextChar] == 0) { |
| |
| hashSet[nextChar] = 1; |
| right++; |
| |
| if ((right - left + 1) > res) res = right - left + 1; |
| } else { |
| |
| while (left <= right && s[left] != nextChar) { |
| |
| hashSet[s[left]] = 0; |
| left++; |
| } |
| |
| |
| left++; |
| right++; |
| } |
| } |
| return res; |
| } |
| int *findAnagrams(char *s, char *p, int *returnSize) { |
| *returnSize = 0; |
| int lenS = strlen(s); |
| int lenP = strlen(p); |
| if (lenS < lenP) return NULL; |
| |
| int *res = (int *) malloc(sizeof(int) * (lenS - lenP + 1)); |
| int *count = (int *) calloc(26, sizeof(int)); |
| |
| int differ = 0; |
| for (int i = 0; i < lenP; ++i) { |
| |
| count[p[i] - 'a']++; |
| |
| count[s[i] - 'a']--; |
| } |
| |
| |
| for (int i = 0; i < 26; ++i) |
| if (count[i] != 0) differ++; |
| |
| if (differ == 0) res[(*returnSize)++] = 0; |
| |
| |
| int left = 1, right = lenP; |
| |
| while (right < lenS) { |
| |
| |
| if (count[s[left - 1] - 'a'] == 0) differ++; |
| count[s[left - 1] - 'a']++; |
| |
| if (count[s[left - 1] - 'a'] == 0) differ--; |
| |
| |
| if (count[s[right] - 'a'] == 0) differ++; |
| count[s[right] - 'a']--; |
| if (count[s[right] - 'a'] == 0) differ--; |
| |
| if (differ == 0) res[(*returnSize)++] = left; |
| left++; |
| right++; |
| } |
| |
| return res; |
| } |
子串
| |
| int subarraySum(int *nums, int numsSize, int k) { |
| int res = 0; |
| for (int i = 0; i < numsSize; ++i) { |
| int tempSum = 0; |
| for (int j = i; j < numsSize; ++j) { |
| tempSum += nums[j]; |
| if (tempSum == k) res++; |
| } |
| } |
| return res; |
| } |
| |
| int subarraySum(int *nums, int numsSize, int k) { |
| int *prefixSum = (int *) calloc(numsSize + 1, sizeof(int)); |
| for (int i = 1; i <= numsSize; ++i) { |
| prefixSum[i] = prefixSum[i - 1] + nums[i - 1]; |
| } |
| |
| int res = 0; |
| for (int i = 0; i < numsSize; ++i) { |
| for (int j = i; j < numsSize; ++j) { |
| if (prefixSum[j + 1] - prefixSum[i] == k) res++; |
| } |
| } |
| return res; |
| } |
| |
| |
| int subarraySum(int *nums, int numsSize, int k) { |
| int res = 0; |
| const int size = 20000 * 1000; |
| |
| int *hashMap = (int *) calloc(size * 2, sizeof(int)); |
| |
| |
| |
| |
| int *prefixSum = (int *) calloc(numsSize + 1, sizeof(int)); |
| |
| hashMap[0 + size]++; |
| for (int i = 1; i <= numsSize; ++i) { |
| prefixSum[i] = prefixSum[i - 1] + nums[i - 1]; |
| res += hashMap[prefixSum[i] - k + size]; |
| |
| hashMap[prefixSum[i] + size]++; |
| } |
| |
| return res; |
| } |
普通数组
| int maxSubArray(int *nums, int numsSize) { |
| int res = nums[0]; |
| |
| |
| |
| int dp[numsSize]; |
| dp[0] = nums[0]; |
| for (int i = 1; i < numsSize; ++i) { |
| |
| if (dp[i - 1] > 0) |
| dp[i] = dp[i - 1] + nums[i]; |
| else |
| dp[i] = nums[i]; |
| if (dp[i] > res) res = dp[i]; |
| } |
| return res; |
| } |
| |
| int maxSubArray(int *nums, int numsSize) { |
| int res = nums[0]; |
| |
| |
| int pre = nums[0]; |
| for (int i = 1; i < numsSize; ++i) { |
| |
| if (pre > 0) |
| pre = pre + nums[i]; |
| else |
| pre = nums[i]; |
| if (pre > res) res = pre; |
| } |
| return res; |
| } |
| int max(int a, int b, int c) { |
| int d = a > b ? a : b; |
| return d > c ? d : c; |
| } |
| |
| |
| int maxCrossingSum(int *nums, int left, int mid, int right) { |
| int leftMax = nums[mid]; |
| int rightMax = nums[mid + 1]; |
| |
| int index = mid; |
| int tempMax = 0; |
| |
| while (index >= left) { |
| tempMax += nums[index]; |
| if (tempMax > leftMax) leftMax = tempMax; |
| index--; |
| } |
| |
| index = mid + 1; |
| tempMax = 0; |
| |
| while (index <= right) { |
| tempMax += nums[index]; |
| if (tempMax > rightMax) rightMax = tempMax; |
| index++; |
| } |
| |
| return leftMax + rightMax; |
| } |
| |
| int maxSubArraySum(int *nums, int left, int right) { |
| if (left == right) return nums[left]; |
| |
| int mid = left + ((right - left) >> 1); |
| |
| |
| |
| |
| |
| return max(maxSubArraySum(nums, left, mid), |
| maxSubArraySum(nums, mid + 1, right), |
| maxCrossingSum(nums, left, mid, right)); |
| } |
| |
| |
| int maxSubArray(int *nums, int numsSize) { |
| if (numsSize == 0) return 0; |
| return maxSubArraySum(nums, 0, numsSize - 1); |
| } |
| int cmp(const int **a, const int **b) { |
| return *a[0] > *b[0]; |
| } |
| |
| int **merge(int **intervals, int intervalsSize, int *intervalsColSize, int *returnSize, int **returnColumnSizes) { |
| *returnSize = 0; |
| int **res = (int **) malloc(sizeof(int *) * intervalsSize); |
| *returnColumnSizes = (int *) malloc(sizeof(int) * intervalsSize); |
| for (int i = 0; i < intervalsSize; ++i) { |
| res[i] = (int *) malloc(sizeof(int) * 2); |
| (*returnColumnSizes)[i] = 2; |
| } |
| |
| |
| qsort(intervals, intervalsSize, sizeof(int) * 2, cmp); |
| |
| for (int i = 0; i < intervalsSize; ++i) { |
| int left = intervals[i][0]; |
| int right = intervals[i][1]; |
| |
| while ((i < intervalsSize - 1) && right >= intervals[i + 1][0]) { |
| if (intervals[i + 1][1] > right) right = intervals[i + 1][1]; |
| i++; |
| } |
| res[*returnSize][0] = left; |
| res[*returnSize][1] = right; |
| (*returnSize)++; |
| } |
| return res; |
| } |
| void reverse(int *nums, int left, int right) { |
| int temp; |
| while (left < right) { |
| temp = nums[left]; |
| nums[left] = nums[right]; |
| nums[right] = temp; |
| left++; |
| right--; |
| } |
| } |
| |
| |
| void rotate(int *nums, int numsSize, int k) { |
| k %= numsSize; |
| reverse(nums, 0, numsSize - 1); |
| reverse(nums, 0, k - 1); |
| reverse(nums, k, numsSize - 1); |
| } |
| int *productExceptSelf(int *nums, int numsSize, int *returnSize) { |
| *returnSize = numsSize; |
| int *res = (int *) malloc(sizeof(int) * numsSize); |
| |
| |
| int leftMulArray[numsSize], rightMulArray[numsSize]; |
| leftMulArray[0] = 1; |
| for (int i = 1; i < numsSize; ++i) |
| leftMulArray[i] = leftMulArray[i - 1] * nums[i - 1]; |
| rightMulArray[numsSize - 1] = 1; |
| for (int i = numsSize - 2; i >= 0; i--) |
| rightMulArray[i] = rightMulArray[i + 1] * nums[i + 1]; |
| |
| |
| for (int i = 0; i < numsSize; ++i) |
| res[i] = leftMulArray[i] * rightMulArray[i]; |
| |
| return res; |
| } |
| |
| int *productExceptSelf(int *nums, int numsSize, int *returnSize) { |
| *returnSize = numsSize; |
| int *res = (int *) malloc(sizeof(int) * numsSize); |
| |
| res[0] = 1; |
| |
| for (int i = 1; i < numsSize; ++i) |
| res[i] = res[i - 1] * nums[i - 1]; |
| |
| |
| int rightMul = 1; |
| for (int i = numsSize - 2; i >= 0; i--) { |
| rightMul = rightMul * nums[i + 1]; |
| |
| res[i] = res[i] * rightMul; |
| } |
| return res; |
| } |
| class Solution { |
| public: |
| |
| int firstMissingPositive(vector<int> &nums) { |
| int length = nums.size(); |
| |
| int value; |
| |
| int temp; |
| |
| for (int i = 0; i < length; ++i) { |
| |
| if (nums[i] <= 0 || nums[i] > length) continue; |
| |
| if (nums[i] == i + 1) continue; |
| |
| |
| value = nums[i]; |
| |
| nums[i] = -1; |
| |
| temp = nums[value - 1]; |
| |
| nums[value - 1] = value; |
| |
| |
| while (temp > 0 && temp <= length && nums[temp - 1] != temp) { |
| value = temp; |
| temp = nums[value - 1]; |
| nums[value - 1] = value; |
| } |
| } |
| |
| |
| for (int j = 0; j < length; ++j) |
| if (nums[j] != j + 1) |
| return j + 1; |
| |
| |
| return length + 1; |
| } |
| }; |
| class Solution { |
| public: |
| |
| |
| int firstMissingPositive(vector<int> &nums) { |
| int length = nums.size(); |
| for (int i = 0; i < length; ++i) { |
| |
| while (nums[i] > 0 && nums[i] <= length && nums[nums[i] - 1] != nums[i]) { |
| |
| |
| swap(nums[nums[i] - 1], nums[i]); |
| } |
| } |
| |
| for (int i = 0; i < length; ++i) |
| if (nums[i] != i + 1) |
| return i + 1; |
| |
| return length + 1; |
| } |
| }; |
| class Solution { |
| public: |
| |
| int firstMissingPositive(vector<int> &nums) { |
| int length = nums.size(); |
| |
| for (int &value: nums) |
| if (value <= 0) value = length + 1; |
| for (int i = 0; i < length; ++i) { |
| |
| int value = abs(nums[i]); |
| |
| |
| if (value <= length) nums[value - 1] = -abs(nums[value - 1]); |
| } |
| for (int i = 0; i < length; ++i) |
| if (nums[i] > 0) return i + 1; |
| |
| return length + 1; |
| } |
| }; |
矩阵
| void setZeroes(int **matrix, int matrixSize, int *matrixColSize) { |
| int *hashMapRow = (int *) calloc(matrixSize, sizeof(int)); |
| int *hashMapColumn = (int *) calloc(*matrixColSize, sizeof(int)); |
| for (int i = 0; i < matrixSize; ++i) { |
| for (int j = 0; j < *matrixColSize; ++j) { |
| if (matrix[i][j] == 0) { |
| |
| hashMapRow[i] = 1; |
| hashMapColumn[j] = 1; |
| } |
| } |
| } |
| |
| for (int i = 0; i < matrixSize; ++i) |
| for (int j = 0; j < *matrixColSize; ++j) |
| if (hashMapRow[i] == 1 || hashMapColumn[j] == 1) matrix[i][j] = 0; |
| } |
| |
| void setZeroes(int **matrix, int matrixSize, int *matrixColSize) { |
| bool flagRow = false; |
| bool flagColumn = false; |
| |
| bool flag = matrix[0][0] == 0; |
| for (int i = 0; i < matrixSize; ++i) { |
| for (int j = 0; j < *matrixColSize; ++j) { |
| if (matrix[i][j] == 0) { |
| if (i == 0) { |
| |
| flagRow = true; |
| continue; |
| } |
| if (j == 0) { |
| |
| flagColumn = true; |
| continue; |
| } |
| |
| matrix[0][j] = 0; |
| matrix[i][0] = 0; |
| } |
| } |
| } |
| |
| |
| for (int i = 1; i < matrixSize; ++i) |
| for (int j = 1; j < *matrixColSize; ++j) |
| if (matrix[0][j] == 0 || matrix[i][0] == 0) matrix[i][j] = 0; |
| |
| |
| if (flag || flagRow) |
| for (int i = 0; i < *matrixColSize; ++i) matrix[0][i] = 0; |
| if (flag || flagColumn) |
| for (int i = 0; i < matrixSize; ++i) matrix[i][0] = 0; |
| } |
| int *spiralOrder(int **matrix, int matrixSize, int *matrixColSize, int *returnSize) { |
| *returnSize = matrixSize * (*matrixColSize); |
| int *res = (int *) malloc(sizeof(int) * (*returnSize)); |
| int index = 0; |
| |
| int up = 0, down = matrixSize - 1; |
| |
| int left = 0, right = *matrixColSize - 1; |
| |
| while (true) { |
| |
| for (int i = left; i <= right; ++i) |
| res[index++] = matrix[up][i]; |
| if (++up > down) break; |
| |
| |
| for (int i = up; i <= down; ++i) |
| res[index++] = matrix[i][right]; |
| if (--right < left)break; |
| |
| |
| for (int i = right; i >= left; i--) |
| res[index++] = matrix[down][i]; |
| if (--down < up)break; |
| |
| |
| for (int i = down; i >= up; i--) |
| res[index++] = matrix[i][left]; |
| if (++left > right) break; |
| } |
| |
| return res; |
| } |
| void rotate(int **matrix, int matrixSize, int *matrixColSize) { |
| int temp[matrixSize][matrixSize]; |
| |
| for (int i = 0; i < matrixSize; ++i) { |
| for (int j = 0; j < matrixSize; ++j) { |
| temp[j][matrixSize - 1 - i] = matrix[i][j]; |
| } |
| } |
| |
| |
| for (int i = 0; i < matrixSize; ++i) |
| for (int j = 0; j < matrixSize; ++j) |
| matrix[i][j] = temp[i][j]; |
| } |
| |
| void rotate(int **matrix, int matrixSize, int *matrixColSize) { |
| for (int i = 0; i < matrixSize / 2; ++i) { |
| for (int j = 0; j < (matrixSize + 1) / 2; ++j) { |
| int temp = matrix[i][j]; |
| matrix[i][j] = matrix[matrixSize - j - 1][i]; |
| matrix[matrixSize - j - 1][i] = matrix[matrixSize - i - 1][matrixSize - j - 1]; |
| matrix[matrixSize - i - 1][matrixSize - j - 1] = matrix[j][matrixSize - i - 1]; |
| matrix[j][matrixSize - i - 1] = temp; |
| } |
| } |
| } |
| void swap(int *a, int *b) { |
| int temp = *a; |
| *a = *b; |
| *b = temp; |
| } |
| |
| void rotate(int **matrix, int matrixSize, int *matrixColSize) { |
| |
| for (int i = 0; i < matrixSize / 2; ++i) |
| for (int j = 0; j < matrixSize; ++j) |
| swap(&matrix[i][j], &matrix[matrixSize - 1 - i][j]); |
| |
| |
| for (int i = 0; i < matrixSize; ++i) |
| for (int j = i + 1; j < matrixSize; ++j) |
| swap(&matrix[i][j], &matrix[j][i]); |
| } |
| bool binarySearch(int *array, int left, int right, int target) { |
| int mid; |
| while (left <= right) { |
| mid = left + ((right - left) >> 1); |
| if (array[mid] == target) { |
| return true; |
| } else if (array[mid] < target) { |
| left = mid + 1; |
| } else { |
| right = mid - 1; |
| } |
| } |
| return false; |
| } |
| |
| |
| bool searchMatrix(int **matrix, int matrixSize, int *matrixColSize, int target) { |
| for (int i = 0; i < matrixSize; ++i) |
| if (binarySearch(matrix[i], 0, *matrixColSize - 1, target)) return true; |
| return false; |
| } |
| |
| bool searchMatrix(int **matrix, int matrixSize, int *matrixColSize, int target) { |
| int row = 0, column = *matrixColSize - 1; |
| while (row < matrixSize && column >= 0) { |
| int cur = matrix[row][column]; |
| if (cur == target) |
| return true; |
| else if (cur > target) |
| |
| column--; |
| else |
| |
| row++; |
| } |
| return false; |
| } |
| |
| void display(int **matrix, int matrixSize, int *matrixColSize) { |
| for (int i = 0; i < matrixSize; ++i) { |
| for (int j = 0; j < *matrixColSize; ++j) { |
| printf("%d ", matrix[i][j]); |
| } |
| puts(""); |
| } |
| puts(""); |
| } |
| |
| bool search(int **matrix, int left, int right, int up, int down, int target) { |
| if (left > right || up > down) return false; |
| if (left == right && up == down) return matrix[left][up] == target; |
| if (left == right - 1 && up == down - 1) { |
| return matrix[left][up] == target |
| || matrix[left + 1][up] == target |
| || matrix[left][up + 1] == target |
| || matrix[left + 1][up + 1] == target; |
| } |
| |
| int midRow = up + ((down - up) >> 1); |
| int midColumn = left + ((right - left) >> 1); |
| int cur = matrix[midRow][midColumn]; |
| printf("left=%d right=%d up=%d down=%d midRow=%d midColumn=%d cur=%d\n", left, right, up, down, midRow, midColumn, |
| cur); |
| |
| if (cur == target) return true; |
| if (cur > target) { |
| |
| puts("左上角"); |
| bool a = search(matrix, left, midColumn, up, midRow, target); |
| puts("右上角"); |
| bool b = search(matrix, midColumn + 1, right, up, midRow - 1, target); |
| puts("左下角"); |
| bool c = search(matrix, left, midColumn - 1, midRow + 1, down, target); |
| return a || b || c; |
| |
| |
| |
| } else { |
| |
| puts("右下角"); |
| bool a = search(matrix, midColumn, right, midRow, down, target); |
| puts("右上角"); |
| bool b = search(matrix, midColumn + 1, right, up, midRow - 1, target); |
| puts("左下角"); |
| bool c = search(matrix, left, midColumn - 1, midRow + 1, down, target); |
| return a || b || c; |
| |
| |
| |
| } |
| } |
| |
| bool searchMatrix(int **matrix, int matrixSize, int *matrixColSize, int target) { |
| display(matrix, matrixSize, matrixColSize); |
| return search(matrix, 0, *matrixColSize - 1, 0, matrixSize - 1, target); |
| } |
链表
| |
| struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) { |
| int len1 = 0, len2 = 0; |
| struct ListNode *p = headA, *q = headB; |
| |
| while (p != NULL) { |
| len1++; |
| p = p->next; |
| } |
| while (q != NULL) { |
| len2++; |
| q = q->next; |
| } |
| |
| |
| p = headA; |
| q = headB; |
| if (len1 > len2) |
| for (int i = 0; i < len1 - len2; ++i) |
| p = p->next; |
| else |
| for (int i = 0; i < len2 - len1; ++i) |
| q = q->next; |
| |
| while (p != NULL) { |
| if (p == q)return p; |
| p = p->next; |
| q = q->next; |
| } |
| return NULL; |
| } |
| |
| struct ListNode *reverseList(struct ListNode *head) { |
| struct ListNode *pre = NULL; |
| struct ListNode *next; |
| struct ListNode *cur = head; |
| |
| |
| while (cur != NULL) { |
| next = cur->next; |
| cur->next = pre; |
| pre = cur; |
| cur = next; |
| } |
| return pre; |
| } |
| |
| struct ListNode *reverseList(struct ListNode *head) { |
| |
| if (head == NULL || head->next == NULL) return head; |
| |
| struct ListNode *newHead = reverseList(head->next); |
| head->next->next = head; |
| head->next = NULL; |
| return newHead; |
| } |
| |
| struct ListNode *findMid(struct ListNode *head) { |
| struct ListNode *slow = head; |
| struct ListNode *fast = head; |
| while (fast != NULL && fast->next != NULL) { |
| fast = fast->next->next; |
| slow = slow->next; |
| } |
| return slow; |
| } |
| |
| |
| struct ListNode *reverseList(struct ListNode *head) { |
| struct ListNode *pre = NULL; |
| struct ListNode *cur = head; |
| struct ListNode *next; |
| while (cur != NULL) { |
| next = cur->next; |
| cur->next = pre; |
| pre = cur; |
| cur = next; |
| } |
| return pre; |
| } |
| |
| |
| bool isPalindrome(struct ListNode *head) { |
| struct ListNode *mid = findMid(head); |
| mid = reverseList(mid); |
| |
| struct ListNode *p = head; |
| struct ListNode *q = mid; |
| while (q != NULL) { |
| if (p->val != q->val) return false; |
| p = p->next; |
| q = q->next; |
| } |
| return true; |
| } |
| |
| |
| |
| |
| |
| |
| |
| bool hasCycle(struct ListNode *head) { |
| if (head == NULL || head->next == NULL) return false; |
| |
| struct ListNode *slow = head; |
| struct ListNode *fast = head->next; |
| while (fast != NULL && fast->next != NULL) { |
| if (slow == fast)return true; |
| slow = slow->next; |
| fast = fast->next->next; |
| } |
| return false; |
| } |
| |
| struct ListNode *detectCycle(struct ListNode *head) { |
| |
| |
| |
| |
| |
| |
| struct ListNode *slow = head, *fast = head; |
| while (fast != NULL && fast->next != NULL) { |
| slow = slow->next; |
| fast = fast->next->next; |
| |
| |
| |
| if (slow == fast) { |
| fast = head; |
| |
| while (slow != fast) { |
| slow = slow->next; |
| fast = fast->next; |
| } |
| |
| return slow; |
| } |
| } |
| |
| return NULL; |
| } |
| |
| struct ListNode *mergeTwoLists(struct ListNode *list1, struct ListNode *list2) { |
| |
| if (list1 == NULL) return list2; |
| if (list2 == NULL) return list1; |
| |
| if (list1->val < list2->val) { |
| list1->next = mergeTwoLists(list1->next, list2); |
| return list1; |
| } else { |
| list2->next = mergeTwoLists(list1, list2->next); |
| return list2; |
| } |
| } |
| |
| struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) { |
| struct ListNode *n1 = (l1); |
| struct ListNode *n2 = (l2); |
| struct ListNode *dummyHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| dummyHead->next = NULL; |
| struct ListNode *pre = dummyHead; |
| |
| |
| int carry = 0; |
| while (n1 != NULL && n2 != NULL) { |
| struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| node->next = NULL; |
| |
| node->val = (n1->val + n2->val + carry) % 10; |
| carry = (n1->val + n2->val + carry) / 10; |
| |
| pre->next = node; |
| pre = pre->next; |
| n1 = n1->next; |
| n2 = n2->next; |
| } |
| while (n1 != NULL) { |
| struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| node->next = NULL; |
| node->val = (n1->val + carry) % 10; |
| carry = (n1->val + carry) / 10; |
| pre->next = node; |
| pre = pre->next; |
| n1 = n1->next; |
| } |
| while (n2 != NULL) { |
| struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| node->next = NULL; |
| node->val = (n2->val + carry) % 10; |
| carry = (n2->val + carry) / 10; |
| pre->next = node; |
| pre = pre->next; |
| n2 = n2->next; |
| } |
| |
| if (carry == 1) { |
| struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| node->next = NULL; |
| node->val = 1; |
| pre->next = node; |
| } |
| return (dummyHead->next); |
| } |
| struct ListNode *removeNthFromEnd(struct ListNode *head, int n) { |
| struct ListNode *dummyHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| dummyHead->next = head; |
| struct ListNode *slow = dummyHead, *fast = dummyHead; |
| |
| int count = n + 1; |
| while (count-- > 0) fast = fast->next; |
| while (fast != NULL) { |
| slow = slow->next; |
| fast = fast->next; |
| } |
| |
| slow->next = slow->next->next; |
| return dummyHead->next; |
| } |
| struct ListNode *swapPairs(struct ListNode *head) { |
| if (head == NULL || head->next == NULL) return head; |
| struct ListNode *nextNode = head->next->next; |
| struct ListNode *right = head->next; |
| right->next = head; |
| head->next = swapPairs(nextNode); |
| return right; |
| } |
| class Solution { |
| public: |
| |
| ListNode *reverseList(ListNode *head, ListNode *tail) { |
| ListNode *pre = tail->next, *cur = head, *next; |
| ListNode *nextNode = tail->next; |
| while (cur != nullptr && cur != nextNode) { |
| next = cur->next; |
| cur->next = pre; |
| pre = cur; |
| cur = next; |
| } |
| |
| return pre; |
| } |
| |
| ListNode *reverseKGroup(ListNode *head, int k) { |
| |
| ListNode *dummyHead = new ListNode(0, head); |
| ListNode *pre = dummyHead; |
| ListNode *left = dummyHead; |
| ListNode *right; |
| |
| int count; |
| |
| while (pre->next != nullptr) { |
| |
| left = pre->next; |
| right = left; |
| |
| for (count = k - 1; count != 0 && right->next != nullptr; count--) { |
| right = right->next; |
| } |
| |
| if (count != 0) return dummyHead->next; |
| |
| pre->next = reverseList(left, right); |
| |
| pre = left; |
| } |
| |
| return dummyHead->next; |
| } |
| }; |
| struct Node *copyRandomList(struct Node *head) { |
| if (head == NULL) return NULL; |
| struct Node *pre = head; |
| |
| |
| while (pre != NULL) { |
| struct Node *node = (struct Node *) malloc(sizeof(struct Node)); |
| node->val = pre->val; |
| |
| node->next = pre->next; |
| pre->next = node; |
| pre = pre->next->next; |
| } |
| |
| pre = head; |
| while (pre != NULL) { |
| |
| if (pre->random != NULL) |
| pre->next->random = pre->random->next; |
| else |
| pre->next->random = NULL; |
| pre = pre->next->next; |
| } |
| |
| pre = head; |
| struct Node *res = head->next, *cur = head->next; |
| while (cur != NULL && cur->next != NULL) { |
| |
| pre->next = pre->next->next; |
| pre = pre->next; |
| |
| cur->next = cur->next->next; |
| cur = cur->next; |
| } |
| |
| pre->next = NULL; |
| return res; |
| } |
| |
| struct ListNode *merge(struct ListNode *l1, struct ListNode *l2) { |
| if (l1 == NULL || l2 == NULL) return l1 == NULL ? l2 : l1; |
| struct ListNode *dummyHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| struct ListNode *pre = dummyHead; |
| while (l1 != NULL && l2 != NULL) { |
| if (l1->val < l2->val) { |
| pre->next = l1; |
| l1 = l1->next; |
| } else { |
| pre->next = l2; |
| l2 = l2->next; |
| } |
| pre = pre->next; |
| } |
| |
| if (l1 != NULL) pre->next = l1; |
| if (l2 != NULL) pre->next = l2; |
| return dummyHead->next; |
| } |
| |
| struct ListNode *sortList(struct ListNode *head) { |
| |
| int len = 0; |
| struct ListNode *temp = head; |
| while (temp != NULL) { |
| len++; |
| temp = temp->next; |
| } |
| |
| struct ListNode *dummyHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| dummyHead->next = head; |
| |
| |
| for (int gap = 1; gap < len; gap <<= 1) { |
| struct ListNode *pre = dummyHead; |
| struct ListNode *cur = dummyHead->next; |
| |
| |
| while (cur != NULL) { |
| |
| struct ListNode *l1 = cur; |
| int i = 1; |
| while (i < gap && cur->next != NULL) { |
| cur = cur->next; |
| i++; |
| } |
| |
| |
| struct ListNode *l2 = cur->next; |
| |
| cur->next = NULL; |
| |
| |
| |
| cur = l2; |
| i = 1; |
| while (i < gap && cur != NULL && cur->next != NULL) { |
| cur = cur->next; |
| i++; |
| } |
| |
| |
| struct ListNode *next = NULL; |
| |
| if (cur != NULL) { |
| |
| next = cur->next; |
| |
| cur->next = NULL; |
| } |
| |
| |
| pre->next = merge(l1, l2); |
| |
| while (pre->next != NULL) { |
| pre = pre->next; |
| } |
| |
| cur = next; |
| } |
| } |
| |
| return dummyHead->next; |
| } |
| class Solution { |
| public: |
| |
| ListNode *mergeKLists(vector<ListNode *> &lists) { |
| |
| if (lists.empty()) return nullptr; |
| ListNode *dummyHead = new ListNode(0, nullptr); |
| ListNode *pre = dummyHead; |
| int length = lists.size(); |
| |
| while (true) { |
| |
| int minHeadIndex = -1; |
| for (int i = 0; i < length; ++i) { |
| |
| if ((lists[i] != nullptr) && |
| (minHeadIndex == -1 || (lists[i]->val < lists[minHeadIndex]->val))) |
| minHeadIndex = i; |
| } |
| |
| |
| if (minHeadIndex == -1) return dummyHead->next; |
| |
| ListNode *node = lists[minHeadIndex]; |
| lists[minHeadIndex] = lists[minHeadIndex]->next; |
| node->next = nullptr; |
| pre->next = node; |
| pre = pre->next; |
| |
| |
| int finished = 0; |
| for (auto &curHead: lists) |
| if (curHead == nullptr) finished++; |
| if (finished >= length - 1) break; |
| } |
| |
| |
| for (auto &curHead: lists) { |
| if (curHead != nullptr) { |
| pre->next = curHead; |
| return dummyHead->next; |
| } |
| } |
| |
| return dummyHead->next; |
| } |
| }; |
| class Solution { |
| public: |
| |
| ListNode *mergeTwoLists(ListNode *a, ListNode *b) { |
| if ((!a) || (!b)) return a ? a : b; |
| ListNode *dummyHead = new ListNode(0, nullptr); |
| ListNode *pre = dummyHead; |
| ListNode *p = a, *q = b; |
| while (p && q) { |
| if (p->val < q->val) { |
| pre->next = p; |
| p = p->next; |
| } else { |
| pre->next = q; |
| q = q->next; |
| } |
| pre = pre->next; |
| } |
| pre->next = (p ? p : q); |
| return dummyHead->next; |
| } |
| |
| |
| ListNode *mergeKLists(vector<ListNode *> &lists) { |
| ListNode *res = nullptr; |
| for (size_t i = 0; i < lists.size(); ++i) |
| res = mergeTwoLists(res, lists[i]); |
| return res; |
| } |
| }; |
| class Solution { |
| public: |
| |
| ListNode *mergeTwoLists(ListNode *a, ListNode *b) { |
| if ((!a) || (!b)) return a ? a : b; |
| ListNode *dummyHead = new ListNode(0, nullptr); |
| ListNode *pre = dummyHead; |
| ListNode *p = a, *q = b; |
| while (p && q) { |
| if (p->val < q->val) { |
| pre->next = p; |
| p = p->next; |
| } else { |
| pre->next = q; |
| q = q->next; |
| } |
| pre = pre->next; |
| } |
| pre->next = (p ? p : q); |
| return dummyHead->next; |
| } |
| |
| ListNode *merge(vector<ListNode *> &lists, int left, int right) { |
| if (left > right) return nullptr; |
| if (left == right) return lists[left]; |
| int mid = (left + right) >> 1; |
| return mergeTwoLists(merge(lists, left, mid), merge(lists, mid + 1, right)); |
| } |
| |
| |
| ListNode *mergeKLists(vector<ListNode *> &lists) { |
| return merge(lists, 0, lists.size() - 1); |
| } |
| }; |
| class Solution { |
| public: |
| |
| static bool cmp(ListNode *a, ListNode *b) { |
| return (*a).val > (*b).val; |
| } |
| |
| |
| ListNode *mergeKLists(vector<ListNode *> &lists) { |
| |
| if (lists.empty()) return nullptr; |
| ListNode *dummyHead = new ListNode(0, nullptr); |
| ListNode *pre = dummyHead; |
| |
| |
| priority_queue<ListNode *, vector<ListNode *>, decltype(&cmp)> heap(cmp); |
| |
| for (auto &curHead: lists) { |
| if (curHead == nullptr) continue; |
| heap.push(curHead); |
| curHead = curHead->next; |
| } |
| |
| while (true) { |
| if (heap.empty() || heap.top() == nullptr) break; |
| |
| ListNode *node = heap.top(); |
| heap.pop(); |
| pre->next = node; |
| pre = pre->next; |
| |
| |
| if (node->next != nullptr) heap.push(node->next); |
| } |
| return dummyHead->next; |
| } |
| }; |
| |
| class LRUCache { |
| ListNode dummyHead; |
| ListNode dummyTail; |
| int capacity; |
| Map<Integer, ListNode> map; |
| |
| public LRUCache(int capacity) { |
| map = new HashMap<>(); |
| this.capacity = capacity; |
| dummyHead = new ListNode(); |
| dummyTail = new ListNode(); |
| dummyHead.next = dummyTail; |
| dummyTail.prev = dummyHead; |
| } |
| |
| public int get(int key) { |
| if (!map.containsKey(key)) return -1; |
| ListNode node = map.get(key); |
| moveToHead(node); |
| return node.value; |
| } |
| |
| public void addToHead(ListNode node) { |
| map.put(node.key, node); |
| node.next = dummyHead.next; |
| dummyHead.next.prev = node; |
| dummyHead.next = node; |
| node.prev = dummyHead; |
| } |
| |
| public void moveToHead(ListNode node) { |
| |
| node.prev.next = node.next; |
| node.next.prev = node.prev; |
| |
| node.next = dummyHead.next; |
| dummyHead.next.prev = node; |
| node.prev = dummyHead; |
| dummyHead.next = node; |
| } |
| |
| public void deleteNode(ListNode node) { |
| map.remove(node.key); |
| node.prev.next = node.next; |
| node.next.prev = node.prev; |
| } |
| |
| public void put(int key, int value) { |
| if (map.containsKey(key)) { |
| ListNode node = map.get(key); |
| node.value = value; |
| moveToHead(node); |
| return; |
| } |
| |
| if (map.size() == capacity) deleteNode(dummyTail.prev); |
| |
| ListNode node = new ListNode(key, value); |
| addToHead(node); |
| } |
| } |
| |
| class ListNode { |
| int key; |
| int value; |
| ListNode prev; |
| ListNode next; |
| |
| public ListNode(int key, int value, ListNode prev, ListNode next) { |
| this.key = key; |
| this.value = value; |
| this.prev = prev; |
| this.next = next; |
| } |
| |
| public ListNode(int key, int value) { |
| this.key = key; |
| this.value = value; |
| } |
| |
| public ListNode() { |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步