随笔分类 - c++
摘要:Reference:浙大数据结构慕课 1、非递归中序遍历 1)定义一个栈。 2)一直向左节点访问,将访问到的节点入栈,直到节点为空。 3)栈顶pop出一个节点,指针指向其右节点,重复上述过程。 code: 1 bool inOrder(TreeNode *root) { 2 stack<TreeNo
阅读全文
摘要:出现的语法错误或者需记住的点: C++: 1、不是指针用“.”而不是“->”。 2、且用"&&"而不是“&”。 3、set的查找:s.find(sth) != s.end() 4、string取子串:s.substr(begin, len),注意第二个参数是截取子串的长度,不是结束位置! 5、str
阅读全文
摘要:1753题目链接 题目大意: 一个4乘4的棋盘,上面放满了正反两面分别为黑和白的棋子,翻转一个棋子会让这个棋子上下左右的棋子也翻转,给定一个初始状态,求使所有棋子颜色相同所需的最少翻转次数。 解题思路: 先检查翻转0个棋子时是否所有棋子颜色一致,若不一致则翻转1个棋子,依次类推,若翻转某n个棋子后成
阅读全文
摘要:Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00
阅读全文
摘要:1、binary search tree Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definitio
阅读全文
摘要:一种方法是用set存储出现过的指针,重复出现时就是有环: 1 class Solution { 2 public: 3 bool hasCycle(ListNode *head) { 4 set<ListNode*> st; 5 ListNode *p = head; 6 while(p) { 7
阅读全文
摘要:Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runti
阅读全文
摘要:1、默认为大顶堆 运行结果: 2、小顶堆 运行结果: 3、自定义 运行结果:
阅读全文
摘要:Given a sequence of positive integers and another positive integer p. The sequence is said to be a perfect sequence if M≤m×p where M and m are the max
阅读全文
摘要:When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A socia
阅读全文
摘要:第一次参加pat考试,结果很惨,只做了中间两道题,还有一个测试点错误,所以最终只得了不到50分。题目是甲级练习题的1148-1151。 考试时有些紧张,第一题第二题开始测试样例都运行不正确,但是调试程序考场的vs2013不能粘贴,还得一点点输上去。浪费了很多时间。 1、Werewolf - Simp
阅读全文
摘要:Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making
阅读全文
摘要:Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle in the early years. When a boy A had a crush on a g
阅读全文
摘要:1、 char c = getchar(); 输入单个字符,可输入空格、换行符。 2、 cin >> s; 不读取空格或换行符。 3、 getline(cin, s); 输入一行到字符串s,输入包含空格,会读取换行符但是不输入到字符串中。 例: 先输入一个整数,再读取一行到字符串s,再读取一行中空格
阅读全文
摘要:An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any
阅读全文
摘要:This time, you are supposed to help us collect the data for family-owned property. Given each person's family members, and the estate(房产)info under hi
阅读全文
摘要:A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, dependi
阅读全文
摘要:Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to ou
阅读全文
摘要:1、printf string型字符串 1 printf("%s", s.c_str()); 输入string型字符串 1 cin >> s; 而不能用 1 scanf_s("%s", &s); s1.compare(s2): 相等返回0;s1 > s2 返回 1;s1 < s2 返回-1. 2、m
阅读全文