classSolution { public: boolfindNumberIn2DArray(vector<vector<int>>& matrix, int target){ if (matrix.empty()) returnfalse; int column = matrix[0].size(); int row = matrix.size(); if (column <= 0 || row <= 0) returnfalse; int r = 0 ; int c = column - 1;
while( r < row && c >= 0 ){ if (matrix[r][c] == target){ returntrue; }elseif(matrix[r][c] > target){ c --; }else{ r ++; } } returnfalse; } };
} voidappendTail(int value){ s1.push(value); } intdeleteHead(){ int ans; if(s1.empty() && s2.empty() )return-1; if (s2.empty()){ while(!s1.empty()){ s2.push(s1.top()); s1.pop(); } } ans = s2.top(); s2.pop(); return ans; } };
/** * Your CQueue object will be instantiated and called as such: * CQueue* obj = new CQueue(); * obj->appendTail(value); * int param_2 = obj->deleteHead(); */
classMinStack { private: stack<int> common; stack<int> assist; public: /** initialize your data structure here. */ MinStack() {
} voidpush(int x){ common.push(x); if (assist.empty()) assist.push(x); else{ int top = assist.top(); if ( x < top ) assist.push(x); else assist.push(top); }
/** * Your MinStack object will be instantiated and called as such: * MinStack* obj = new MinStack(); * obj->push(x); * obj->pop(); * int param_3 = obj->top(); * int param_4 = obj->min(); */
classSolution: defmaxValue(self, grid: List[List[int]]) -> int: row, col = len(grid), len(grid[0]) # print(row, col ) # 初始化第一列 for r in range(1, row): grid[r][0] += grid[r-1][0] # 初始化第一行 for c in range(1, col): grid[0][c] += grid[0][c - 1] # dp状态更新 for i in range(1, row): for j in range(1, col): grid[i][j] += max(grid[i-1][j], grid[i][j - 1]) return grid[row - 1][col - 1]
classSolution: deffirstUniqChar(self, s: str) -> str: hashtable = OrderedDict() for i in s: hashtable[i] = hashtable.setdefault(i, 0) + 1 for k, v in hashtable.items(): if v == 1: return k return' '
// 找到和target相等的最边界(target 最小的下标) intgetLeft(vector<int>& v, int target){ int len = v.size(); int l = 0, r = len-1; while( l <= r){ int m = l + r >> 1; if ( v[m] < target){ l = m + 1; }else{ // 如果相等的话缩小右边边界 r = m - 1; } } return l; } // 找到和target的最右界(比target大的最小下标) intgetRight(vector<int>& v, int target){ int len = v.size(); int l = 0, r = len-1; while( l <= r){ int m = l + r >> 1; if ( v[m] <= target){ // 如果相等的话, 说明之前都是正确的, 那么就需要缩小左边界的范围 l = m + 1; }else{ r = m - 1; } } return r; }
classSolution: defmaxSlidingWindow(self, nums: List[int], k: int) -> List[int]: ans = [] ifnot nums: return [] for i in range(len(nums) - k + 1): ans.append(max(nums[i: i + k]))
classSolution { public: vector<double> twoSum(int n) { int dp[15][70]; memset(dp, 0, sizeof(dp)); for (int i = 1; i <= 6; i ++) { dp[1][i] = 1; } for (int i = 2; i <= n; i ++) { for (int j = i; j <= 6*i; j ++) { for (int cur = 1; cur <= 6; cur ++) { if (j - cur <= 0) { break; } dp[i][j] += dp[i-1][j-cur]; } } } int all = pow(6, n); vector<double> ret; for (int i = n; i <= 6 * n; i ++) { ret.push_back(dp[n][i] * 1.0 / all); } return ret; } };
# 另一种输入方式 # for line in sys.stdin: # ele = line.split('\\')[-1].strip('\n') # if ele not in lst: # lst.append(ele) # if ele in dct: # dct[ele] = dct[ele] + 1 # else: # dct[ele] = 1
whileTrue: try: ins = input().strip() path = ins.split('\\')[-1] if path notin rec_dict: rec_dict[path] = 1 else: rec_dict[path] += 1 except EOFError: break # print(rec_dict) sort_res = sorted(rec_dict.items(), key = lambda d: d[1], reverse=True) # print(rec_dict) for k, v in sort_res[:8]: path, linenum = k.split() print(path[-16:], linenum, v)
Tips: 两种输入方式的推出都为:ctrl + z
EOF是一个计算机术语,为End Of File的缩写,在操作系统中表示资料源无更多的资料可读取。资料源通常称为档案或串流。
而在不同系统的EOF所代表的值是不一样的,在Visual Studio 2017下为ctrl+c,windows下为ctrl+z,linux/unix下为ctrl+c或ctrl+d;
voidmerge(int *arr, int l, int mid, int r){ int begin1, begin2; begin1 = l; begin2 = mid+1; int index; // 新数组添加元素的索引值 // int *newarr = (int *)malloc((r-l+1)*sizeof(int)); int *newarr = newint[r-l+1];
#include<iostream> usingnamespacestd; voidquickSort(int a[], int m,int n); intpartion(int a[], int m, int n); intmain() { int a[] = { 6,1,2,7,9,3,4,5,10,8 }; int m = 0; int n = (sizeof(a) / 4)-1; quickSort(a, m,n); for (int i = 0; i < 10; i++) { cout << a[i] << " "; } } voidquickSort(int a[], int m, int n) { if (m < n) { int q = partion(a, m, n); quickSort(a, m, q ); quickSort(a, q + 1, n); } } intpartion(int a[], int m, int n) { int key=m; int j= n,i=m; int temp1, temp2; while (i != j) { while (a[j] > a[key] && i < j) { --j; } while ((a[i] < a[key]) && (i < j)) { ++i; }if (i < j) { temp1 = a[j]; a[j] = a[i]; a[i] = temp1; } } temp2 = a[key]; a[key] = a[i]; a[i] = temp2; return i; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
voidquickSort(int arr[], int l, int r){ if ( l >= r) return; int i = l, j = r; int key = l + r >> 1; int x = arr[key]; while ( i < j){ while (arr[j] > x ) j --; while (arr[i] < x) i++; if (i < j){ swap(arr, i, j); } quickSort(arr, l, j); quickSort(arr, j+1, r); } }
上述两种有重复元素就爬了
1 2 3 4 5 6 7 8 9 10 11 12 13
voidquick_sort(int q[], int l, int r) { if (l >= r) return;
int i = l - 1, j = r + 1, x = q[l + r >> 1]; while (i < j) { do i ++ ; while (q[i] < x); do j -- ; while (q[j] > x); if (i < j) swap(q[i], q[j]); } quick_sort(q, l, j), quick_sort(q, j + 1, r); }
记录下C++的split写法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<bits/stdc++.h> usingnamespacestd;
intmain(){ string s; getline(cin, s); // cout << s << endl; int dindex = s.find('-'); cout << dindex<< endl; string a = s.substr(0, dindex); string b = s.substr(dindex+1); cout << a << "*" << b; cout << set return0; }
def removeElement(nums, val): # print(nums) nums = list(map(str, nums)) st = ''.join(nums) st = st.replace(str(val), '') return list(map(int, st))
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution: @staticmethod deffirstUniqChar( s: str) -> str: hashtable = OrderedDict() ans = '' for i in s: hashtable[i] = hashtable.setdefault(i, 0) + 1 for k, v in hashtable.items(): if v == 1: return k
res = Solution.firstUniqChar("abaccdeff") print(res)
// 去重 int last; if ( ans.empty() ) last = 1; else last = ans.back(); for (int i = last; i <= val/2 ; i++) { ans.push_back(i); func(val-i, n-1); ans.pop_back(); } }
// way2 // void func(int val, int n, int start){ // if (n == 1){ // ans.push_back(val); // idx ++; // cout << "Case:" << idx << endl; // printRes(); // cout << "==========" << endl; // ans.pop_back(); // return ; // } // for (int i = start; i <= val/2 ; i++) { // ans.push_back(i); // func(val-i, n-1, i); // ans.pop_back(); // } // }