MacOS fatal error: wchar.h: No such file or directory MacOS 用 g++ 上编译 C++ 时报错,解决方法 打开控制台,运行 xcode-select --install 到桌面,可以看到对话框,点击同意,进行下一步…… 安装好后,即可。
C++ Linux中C++编译、使用动态链接库 Linux中so为共享动态链接库。下面我们通过一个四则运算的实例来学习动态链接库。 首先是头文件:my_math.h: /** * my_math.h */ // a + b int Add(int a, int b); // a - b int Minus(int a, int b); // a * b int Multiply(int a, int
leetcode 【LeetCode】292. Nim Game 问题链接:https://leetcode.com/problems/nim-game/ 尼姆游戏,一堆石子放在桌子上,两个人轮流拿,一次可以拿1-3颗石子,谁拿走最后一颗谁就赢了。 如4颗石子时,先手无论拿几颗,都会输了博弈,因为他至少得拿1颗,剩下的就是3颗,后手可一次全拿获胜。所以如果一堆石子放在桌子上,如果轮到这个人的时候面对的是4颗石子,那他肯定输,而另一个人肯定赢。 代码: public boolean canWinNim(int n) { return !(n%4==0); }
leetcode 【LeetCode】290. Word Pattern 问题链接:https://leetcode.com/problems/word-pattern/ 思路: 使用一个map存储映射关系即可 public boolean wordPattern(String pattern, String str) { String first = pattern; String[] second = str.split(" "); if(first.length()!=second.length) { return false; } Map m
leetcode 【LeetCode】283. Move Zeroes 问题链接:https://leetcode.com/problems/move-zeroes/ 思路: 1. 直观版本,遍历,如果一个数是0,就把它后面的所有不为0的数往前挪。O(n2) public void moveZeroes(int[] nums) { int c = 0; for(int i=0;i
leetcode 【LeetCode】278. First Bad Version 问题描述 You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based
leetcode 【LeetCode】263. Ugly Number 问题描述 Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly
leetcode 【LeetCode】258. Add Digits 问题描述 Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2.
leetcode 【LeetCode】257. Binary Tree Paths 问题描述 Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 输出二叉树所有从根节点到叶子节点的路径。递归即可。 代码 class
leetcode 【LeetCode】242. Valid Anagram 问题描述 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false.
leetcode 【LeetCode】237. Delete Node in a Linked List 问题描述 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 ->
leetcode 【LeetCode】235. Lowest Common Ancestor of a 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 definition of LCA on Wikipedia: “The lowest common ancestor is
leetcode 【LeetCode】234. Palindrome Linked List 问题描述 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 问题分析 使用快慢指针找到中点,然后将后半截链表逆置,再将后半截与前半截进行比较,如果有一个数不相等则返回false;否则返回true。 代码
leetcode 【LeetCode】232. Implement Queue using Stacks 问题描述 Implement the following operations of a queue using stacks. * push(x) -- Push element x to the back of queue. * pop() -- Removes the element from in front of queue. * peek() --
leetcode 【LeetCode】231. Power of Two 问题描述 Given an integer, write a function to determine if it is a power of two. 判断一个数是否为2的次方数 思路 1. 当n>1时迭代,如果n%2!=0,即n不是2的倍数,直接false,否则令n=n/2,继续下一轮迭代。结束循环后,若n=1则为次方数。
leetcode 【LeetCode】226. Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was inspired by this original tweet by Max Howell: > Google: 90%
leetcode 【LeetCode】225. Implement Stack using Queues 问题描述 Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top
leetcode 【LeetCode】223. Rectangle Area Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Rectangle Area
leetcode 【LeetCode】219. Contains Duplicate II 问题描述 Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute
leetcode 【LeetCode】217. Contains Duplicate 问题描述 Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false
leetcode 【LeetCode】205. Isomorphic Strings 问题描述 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must
leetcode 【LeetCode】204. Count Primes 问题描述 Description: Count the number of prime numbers less than a non-negative number, n. 点击查看问题 [https://leetcode.com/problems/count-primes/] 问题分析 筛选法求素数,参考这篇文章【算法】普通方法和筛选法求素数 [http://www.zgljl2012.com/suan-fa-pu-tong-fang-fa-he-shai-xuan-fa-qiu-su-shu/] ,这道题用普通方法求素数会超时,只能用筛选法。 代码
leetcode 【LeetCode】206. Reverse Linked List 问题描述 Reverse a singly linked list. 问题分析 链表反转问题。从第二个节点开始,不断把next节点加到head前面。 代码 class Solution { public: ListNode* reverseList(ListNode* head) { ListNode* p = head; ListNode* q = 0; if(head==0) return 0; while(p->next) { q
leetcode 【LeetCode】203. Remove Linked List Elements 问题描述 Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1
leetcode 【LeetCode】202. Happy Number 问题描述 Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum