- 博客(32)
- 收藏
- 关注
原创 结构体的大小还能动态变化?你听过柔性数组吗?
在刚开始写C代码的时候,我们可能会遇到这样一种情况:要存储一组同学的名字,可是每个同学的姓名长度不同,用固定大小的数组会很不方便而且会存在浪费内存的情况。这个时候我们引入了动态数组,通过malloc()函数申请内存按需分配大小。这时候问题来了,如果有一个学生的结构体需要存储姓名这样的属性我们该怎么做呢?同样的我们会在结构体中加入一个char*的指针成员.struct Student { int id; int age; char sex; char* name;}这样
2020-11-30 21:07:13
1839
原创 用宏定义获取枚举值的字符串
用宏定义获取枚举值的字符串最近在学习有限状态机的时候,遇到了一个问题,如何获取枚举类型的枚举值对应的字符串?第一反应就是就是创建一个字符串的映射数组。这样做确实能够达到效果,但是始终觉得不太好看。最后终于在StackOverFlow上找到了一个类似的问题,下面我介绍一下最佳答案,代码如下:代码段一: 作为头文件引入公共宏的部分/// enumFactory.h// expansion macro for enum value definition#define ENUM_VALUE(name,as
2020-11-23 20:37:58
970
原创 大数相乘算法杂谈
title: 大数相乘大数相乘算法杂谈描述:求两个位数少于100位的大整数的乘积。分析:由于整数过大,超出了基本类型的表示范围。因此采用字符串存储大整数,模拟整数乘法求出乘积。长乘法长乘法优化长乘法优化一、长乘法长乘法即我们在小学学习的竖式乘法,以23958233*5830为例 23958233 × 5830 ——————————————— 00000000 ( = 23,958,233 × 0) .
2020-11-21 11:28:31
618
原创 105. Construct Binary Tree from Preorder and Inorder Traversal
Description: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 分析:这道题是典型的分治问题。 eg.pre:[1,2,4,8,5,3,6,9,7], i
2018-01-13 13:23:36
335
翻译 std::vector::rbegin倒序迭代器
begin()和end()是顺序迭代,rbegin()和rend()是倒序迭代。详http://www.cplusplus.com/reference/vector/vector/rbegin/。 下面是一个实例:// vector::rbegin/rend#include <iostream>#include <vector>int main (){ std::vector<int>
2018-01-12 14:00:15
3027
转载 C++之emplace_back() VS push_back()
最近在提交的时候发现同样的代码别人比我的快,原因就是我代码中的push_back()而别人用的是emplace_back(). 于是我上cppreference.com了解了一下emplace_back()。 使用push_back()的时候,会首先构造一个元素,然后拷贝复制传递给容器。使用emplace_back()时,会在容器所在的内存空间直接构造一个元素,避免了额外的移动或赋值操作。通
2018-01-12 13:51:38
980
原创 如何选择指针中置空?NULL, nullptr, 0?
在使用指针的时候,为了防止野指针,我们要对指针初始置空。以前一直使用*ptr = NULL,其中NULL和0是完全等价的。这就造成了一个潜在的危险,当我们将void f(int*)这个函数重定义一个void f(int)函数。这个时候我们传入参数0, 就无法判断0是int类型还是int*类型。c++11使用nullptr代替了NULL,在上述情况下能避免歧义,不会出现错误。 所以,在支持C+
2018-01-12 13:18:36
782
原创 103. Binary Tree Zigzag Level Order Traversal
Description: Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between). For example: G
2018-01-12 10:58:58
266
原创 102. Binary Tree Level Order Traversal
Description: Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given binary tree [3,9,20,null,null,15,7], 3
2018-01-11 17:36:53
214
原创 95. Unique Binary Search Trees II
Description:Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown
2018-01-09 18:42:20
235
原创 96. Unique Binary Search Trees
Description:Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2
2018-01-09 16:52:59
204
原创 110. Balanced Binary Tree
Description: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node
2018-01-07 03:31:34
569
原创 108. Convert Sorted Array to Binary Search Tree
Description:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is defined as a binary tree in which th
2018-01-07 02:36:41
222
原创 41. First Missing Positive
Description:Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3, and [3,4,-1,1] return 2。Your algorithm should run in O(n) time and uses
2018-01-01 22:58:35
279
原创 25. Reverse Nodes in k-Group
Description:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If
2017-12-30 02:05:30
309
原创 tensorflow 从零入门系列(前言)
最近网络上掀起了一股人工智能的热潮,出于对神经网络的好奇,我决定开始好好学习一下tensorflow(来自google的人工智能开源神器)。在接下来的日子里我会将自己的学习过程记录下来,发表在博客上,希望有意于学习tensorflow的小伙伴大家一起共同学习,交流。我的学习记录的大概主体结构如下:1.介绍所需要的算法。主要分成两部分,第一部分简单介绍算法的作用,以及代码实现
2017-12-29 01:16:53
1139
原创 vector中resize()和reserve()
Mark一下:1.resize(n)和resize(n, t), 调整vector数组的大小,多减少补,后面的t是初始化数值,添加时会调用一次构造函数,如果n的大小大于容器的话,容器的内存分配器将自动重新分配内存,如果内存分配失败,会报错 bad_alloc 。例子:(来自c++官方文档)// resizing vector#include #include int main
2017-12-28 23:49:37
1447
原创 69. Sqrt(x)
Description:Implement int sqrt(int x).Compute and return the square root of x.x is guaranteed to be a non-negative integer.分析:这道题用二分法即可。易出错的地方是数是否越界的处理,我们取high(=x)如果x为INT_MAX的话,在high+low的操作就会造
2017-12-28 15:05:49
766
原创 9. Palindrome Number
Description:Determine whether an integer is a palindrome. Do this without extra space.分析:确定数字是否为回文数。将数字倒置,与原数一致即是回文数;否则,则不是。代码:class Solution {public: bool isPalindrome(int x) {
2017-12-28 15:00:40
200
原创 7. Reverse Integer
Description:Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21Not
2017-12-28 14:14:31
184
原创 4. Median of Two Sorted Arrays
Descripition:There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).Example 1
2017-12-27 20:09:59
223
原创 143. Reorder List
Description:Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reor
2017-12-26 18:27:56
214
原创 c++之getline(istream && is,string&str,char delim)用法及注意事项
最近使用到了getline(istream && is,string&str,char delim), 这个函数的作用将输入流的字符串按照分割符delim为界分割出来。c++网站上的介绍是:从流获取线到字符串:1.从提取的字符是并将其存储到STR直到划界字符DELIM被发现(getline (istream&& is, string& str)默认分割符是“\ n”, )。2.如果
2017-12-26 11:58:04
6450
2
翻译 71. Simplify Path
Description:Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"Corner Cases:Did you consider the case
2017-12-26 11:43:30
198
原创 142. Linked List Cycle II
Description:Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up:Can you solve it without using extra sp
2017-12-26 09:36:32
202
原创 93. Restore IP Addresses
Description:Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255.255.11.135", "255.255.111.35"
2017-12-23 15:27:49
179
原创 win10+tensorflow-gpu环境配置
前言: 最近开始下决心好好的学习tensorflow了,以前用的是cpu版本的tensorflow,装好python3.5直接pip install tensorflow-cpu就可以跑起来了。想着自己是N卡又试了试GPU版本的,其中遇到了不少的坑,结果花了我一天的时间。下面是我的安装以前环境配置的全过程(win10(x64)+Anaconda3(x64)+tensorf
2017-12-22 12:08:54
1070
原创 92. Reverse Linked List II
Description:Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Give
2017-12-21 20:53:32
246
原创 91. Decode Ways
Description:A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determi
2017-12-19 18:03:58
244
原创 90. Subsets II
Description:Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.For
2017-12-18 22:21:30
260
原创 86. Partition List
Description:Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the
2017-12-18 17:12:32
320
原创 82. Remove Duplicates from Sorted List II
Description:------------Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5
2017-12-18 13:23:21
225
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人