- 博客(389)
- 收藏
- 关注
原创 python 打印unicode字符串
1. 如果是用print,那么输出到控制台是看不出来区别的,以下两个print的操作输出都是一样的uni_str = u'哈哈'print(uni_str)print(uni_str.encode('utf-8'))2. 如果要是保存到文件的话,那么是否encode就有区别了# 无法正常工作x = u'哈哈'with open("test.txt", "w") as f: f.write(x)# 可以正常工作with open("test.txt", "w") a
2022-05-30 19:45:19 1874 1
原创 124二叉树中最大路径和
import sysclass Solution(object): ans = -sys.maxsize def maxPathSum(self, root): """ :type root: TreeNode :rtype: int """ self.helper(root) return self.ans def helper(self, root): if r
2021-11-22 10:34:39 343
原创 下一个更大的元素
from collections import dequeclass Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: d = dict() queue = deque() for idx, num in enumerate(nums2): while len(queue) and num
2021-11-17 09:57:53 735
原创 129 根节点到叶子结点总和
犯了一个大错误,讲一个数字类型传进函数中,妄图保存中间结果,python中对于不可变的类型就是值传递,起不到保存结果的作用# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right .
2021-11-13 16:12:18 368
原创 路径总和2
class Solution(object): def pathSum(self, root, targetSum): """ :type root: TreeNode :type targetSum: int :rtype: List[List[int]] """ if root is None: return [] ans = list() cu
2021-11-13 15:48:03 74
原创 236求最近公共祖先
# @lc code=start# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def lowestCommonAncestor(self, root, p,
2021-11-12 21:19:30 345
原创 lc73矩阵置零
class Solution: def setZeroes(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead. """ m = len(matrix) n = len(matrix[0]) if m != 0 else 0 zero_row = list
2021-10-20 00:49:38 123
原创 lc 76
from collections import defaultdictclass Solution: def minWindow(self, s: str, t: str) -> str: hs = defaultdict(int) ht = defaultdict(int) for c in t: ht[c] += 1 l = 0 r = 0 cnt = 0
2021-09-27 23:57:20 90
原创 python利用args接收多参数
def info(x, *args, **kwargs): print(x) print(args) for tmp in args: print(tmp) print(kwargs) for k, v in kwargs.items(): print(k, v)info("hello", "1", "2", "3", m=123, y=456输出hello('1', '2', '3')123{'m':
2021-09-20 21:06:57 555
原创 编程工具反思
我之前学过C、C++、Java、python、shell等语言,之前的思想就是c是最强大的,C++次之,应该重点把握,其他语言要么繁琐、要么没有强数据类型等各种原因,就是觉得不好用。我认为C系列的语言最考验人,需要程序员对编程有较为深刻的理解,可以提高程序员的功力,久而久之,也可能成为一个编程高手。现在思想发生了转变,毕竟工具就是用来完成特定任务的,每种语言的特性、适用情况都是不一样的,完全没必要区分高低贵贱。假设要完成某个任务,用C++可以,比较麻烦;用python也可以,写个脚本就完成了,比较简单;用
2021-09-05 21:30:36 112
原创 leetcode合并区间
class Solution(object): def merge(self, intervals): """ :type intervals: List[List[int]] :rtype: List[List[int]] """ ans = [] if intervals is None or 0 == len(intervals): return [] int
2021-09-05 20:35:30 69
原创 通用数据处理注意问题
1. python print直接输出到,不存缓冲区这一点一定要考虑,过滤器有时候不会直接输出结果,令人头疼小批量测试 100条或者1000条就能测试出来结果python2sys.stdout.flush()python3print(flush=True)2. 请求重试问题python3from urllib3 import Retry, PoolManagerretries = Retry(connect=5, read=2, redirect=5, backof
2021-08-11 11:07:35 77
原创 coin change
#include <iostream>#include <unordered_map>#include <vector>#include <algorithm>using namespace std;struct cmp { bool operator()(const int& a, const int& b) { return a > b; }};void dfs(vector<
2021-06-21 11:29:10 86
原创 322 零钱兑换
解决方案可以考察的点动态对话数组的初始化要求最小值,那么初始化应该是最大值dp[0]的设定class Solution {public: int coinChange(vector<int>& coins, int amount) { if (coins.empty() || amount <= 0) { return 0; } vector<int> dp(am
2021-06-21 09:12:56 99
原创 1190. 反转每对括号间的子串2
解析这种方案比较考察基础操作,要树立清楚思路碰到左括号怎么办将当前tmp字符串放到栈中,然后将tmp变为空,重新开始碰到右括号怎么办就意味着当前的临时字符串应该翻转了,而且还有处理之前放栈中的字符串碰到普通字符怎么办直接添加到临时字符串就可以class Solution {public: string reverseParentheses(string s) { int n = s.length(); if (0 == n
2021-06-12 23:10:58 73
原创 1190. 反转每对括号间的子串
题目解析这个题目的本质就是一个符号匹配问题,而且还是明确告诉这些括号都是匹配的相似的题目应该是判断括号是否匹配思路就是找到一对匹配的括号,然后翻转其中的字符就行了。class Solution {public: string reverseParentheses(string s) { if (s.length() == 0) { return ""; } stack<int> stk;
2021-06-12 22:30:37 92
原创 lc363. 矩形区域不超过 K 的最大数值和
主要用的是二维前缀和注意下标的操作就可以class Solution {public: int maxSumSubmatrix(vector<vector<int>>& matrix, int k) { if (matrix.empty() || matrix[0].size() == 0) { return 0; } int m = matrix.size(); int.
2021-05-31 23:59:37 95
原创 lc 554
一个伪装的很好的哈希表统计频率的题目使用到了前缀数组的思想class Solution {public: int leastBricks(vector<vector<int>>& wall) { unordered_map<int, int> m; int n = wall.size(); // 统计每个边缘的频率,当然要用到前缀和来计算边缘 for (int i = 0; i &.
2021-05-30 12:11:11 57
原创 1190 jjj
class Solution {public: string reverseParentheses(string s) { stack<string> stk; string str; for (int i = 0; i < s.length(); i++) { if (s[i] == '(') { stk.push(str); str = ""
2021-05-30 02:43:35 60
原创 lc 260
主要的点就在于 x ^ (-x) 得到最后一个1的位置利用这两个不同的值的异或值知道有某些位是不一致的,随便找其中的1位(这里为了方便找了最靠近右边的那一位)就可以将这些数字分成两批,两个不同的数字分别出来这两批数据中class Solution {public: vector<int> singleNumber(vector<int>& nums) { long long result = 0; for (auto num.
2021-05-30 01:13:19 119
原创 477汉明距离总和
题目链接输入:nums = [4,14,2]输出:6解释:在二进制表示中,4 表示为 0100 ,14 表示为 1110 ,2表示为 0010 。(这样表示是为了体现后四位之间关系)所以答案为:HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 60 1 0 01 1 1 00 0 1 0 j在j的位置有2个1,1个0,那么最多的出现不同的组合就是 2 .
2021-05-30 00:07:15 63
原创 就是打印菱形
rows = 29full_size = 2 * rows + 1for i in range(rows): star_size = 2 * i + 1 blank_size = (full_size - star_size) // 2 print(" " * blank_size + "*" * star_size)for i in range(rows - 2, -1, -1): star_size = 2 * i + 1 blank_size = (full_size - star_s
2021-05-17 20:40:22 36
原创 位运算的技巧
消除最后一位的1n & (n - 1)10的二进制代码是1010, 9的二进制代码1001, 与操作结果是1000, 最后一位(也就是1010的倒数第二位)的1消失了升级例子: 统计一个数字的二进制中有多少个1int x = -11;int count = 0;while (x) { x = x & (x - 1); count += 1;}...
2021-05-15 13:23:15 58
原创 228 汇总区间
class Solution {public: vector<string> summaryRanges(vector<int>& nums) { int l = 0; int r = 0; vector<string> result; while (r < nums.size()) { // find interval while (r
2021-05-15 12:41:24 90
原创 N叉树的前序遍历
https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal/submissions/现在前序遍历的思路就是根节点,子树1, 子树2,子树3,。。。。子树最后一个转成栈存储的话,栈的处理顺序是先进后出,所以子树的顺序要反过来class Solution {public: vector<int> preorder(Node* root) { stack<Node*> stk;
2021-05-14 09:23:56 86
原创 二叉树的前序遍历
前序遍历的本质就是根 左 右如果考虑使用栈来实现的, 需要先将右孩子存起来,再存左孩子,依次pop的时候就是先处理左孩子,然后是右孩子class Solution {public: vector<int> preorderTraversal(TreeNode* root) { if (nullptr == root) { return vector<int>(); } vector<int.
2021-05-14 09:12:12 66
原创 text文件中的数字字符串转到Excel,取消科学计数法
打开空白excel文档选中所有单元格, 设置单元格格式为文本拷贝所有的text数据,粘贴到excel文档中这样数字不会自动变成科学计数法
2021-05-12 19:52:41 644
原创 lc 152
多添加了一个状态0代表在当前位置为止的最小值1代表在当前位置为止的最大值class Solution {public: int maxProduct(vector<int>& nums) { int n = nums.size(); int result = nums[0]; vector<vector<int>> dp(n, vector<int>(2, 0)); /.
2021-05-08 00:24:35 77
原创 300. 最长递增子序列
class Solution {public: int lengthOfLIS(vector<int>& nums) { int n = nums.size(); vector<int> dp(n, 1); int result = 1; for (int i = 1; i < n; i++) { int j = i - 1; int tmp = 0
2021-05-08 00:22:25 61
原创 防止ctrl + w 关闭终端窗口
https://qastack.cn/apple/44412/disable-command-w-in-the-terminal
2021-05-07 19:31:51 588
原创 lc 724
思路很简单左边的和等于右边的和, 为了防止多次计算,直接累加前缀和然后遍历位置,计算左右两段的和,如果相等就返回下标复杂的点在于精确控制下标,前缀和要比原数组多一位原来i下标对应的累加和在前缀和数组中的位置为i+1给你一个整数数组 nums,请编写一个能够返回数组 “中心下标” 的方法。数组 中心下标 是数组的一个下标,其左侧所有元素相加的和等于右侧所有元素相加的和。如果数组不存在中心下标,返回 -1 。如果数组有多个中心下标,应该返回最靠近左边的那一个。注意:中心.
2021-05-02 22:43:26 71
原创 三数之和
class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { sort(begin(nums), end(nums)); const int n = nums.size(); vector<vector<int>> ans; for (int i = 0; i < n; ++i) { if (i &a
2021-05-02 19:36:55 54
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人