![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
realxuejin
c/c 算法 网络
展开
-
【LeetCode】303 Range Sum Query - Immutable
题目链接Range Sum Query - Immutable分析简单题。加设一个 vector<int> sum sum[i] 代表从第0个数到第i个数之和。#### **代码** class NumArray { public: vector<int> sum; NumArray(vector<int> &nums) {原创 2015-12-11 15:09:17 · 351 阅读 · 0 评论 -
【LeetCode】7 Reverse Integer
题目链接Reverse Integer分析对整数进行反转输出。如果发生了溢出现象,要返回0.代码 class Solution { public: int reverse(int x) { int tmp, y=0; while(x!=0){ tmp = y*10 + x%原创 2015-12-13 14:27:40 · 352 阅读 · 0 评论 -
【LeetCode】292 Nim Game
题目链接Nim Game分析你和朋友共同取一堆石头,每次可以取1-3个。你先取石头,谁最终把石头取完则获胜。简单的找规律题。只要石头的个数是4的倍数则你一定会输。代码class Solution {public: bool canWinNim(int n) { if(n%4==0){ return false; }else{原创 2015-12-13 18:30:59 · 595 阅读 · 0 评论 -
【LeetCode】14 Longest Common Prefix
题目链接Longest Common Prefix分析简单题。寻找string数组中的最长公共字符串。代码 class Solution { public: string longestCommonPrefix(vector<string>& strs) { if(strs.size()==0){ return "原创 2015-12-13 16:42:01 · 401 阅读 · 0 评论 -
【LeetCode】9 Palindrome Number
题目链接String to Integer(atoi)分析输入一个int数字,判断该数字是否满足回文串的特点。 负数的话一律认为不符合代码1 我直接用vector自带的“==”来判断,这方法很便捷,但应该不是题目本意。 class Solution { public: bool isPalindrome(int x) { if(x<0原创 2015-12-13 16:15:45 · 386 阅读 · 0 评论 -
【LeetCode】6 ZigZag Conversion
题目链接ZigZag Conversion分析按行来处理,判断每行的字符在原输入字符串中的距离。 首尾行和中间行情况不同,要分开处理。几个特殊的case: (1)s为空 (2)s.size() <= numRows代码 class Solution { public: string convert(string s, int numRows) {原创 2015-12-13 09:47:33 · 339 阅读 · 0 评论 -
【LeetCode】278 First Bad Version
题目链接First Bad Version分析给出一个数组,如果有一个数字是badversion,则其后的数字都是badversion。要求找出第一个出现badversion的位置。按顺序查找会超时,用二分法就可以轻松过了。代码 // Forward declaration of isBadVersion API. bool isBadVersion(int version);原创 2015-12-13 18:28:31 · 431 阅读 · 0 评论 -
【LeetCode】8 String to Integer(atoi)
题目链接String to Integer(atoi)分析自己实现atoi函数的功能,将字符串转为数字。需要注意的case:前面有空格 Input: ” 010” Expected: 10 中间有字母 Input: ” -0012a42” Expected: -12超出int范围时: Input: “2147483648”原创 2015-12-13 15:41:33 · 323 阅读 · 0 评论 -
【LeetCode】299 Bulls and Cows
题目链接Bulls and Cows分析题目给出俩个字符串secret和guess。找出bull和cow的个数。 bull: 位置相同并且字符相同; cow: 字符相同但是位置不同;注意这个case的输出结果: Input: “1122” “1222” Expected: “3A0B”代码 string num2str(int num){原创 2015-12-11 15:11:04 · 358 阅读 · 0 评论 -
【LeetCode】1 Two Sum
#### 题目链接 Two Sum分析给出一个数组,找出俩个数,二者之和为指定的target。注意点: (1)返回的是二者的下标; (2)数组元素有可能是负数;采用结构体来标记一个节点的值和下标,将节点按照值的大小排序,而后采用二分查找。代码struct node{ node(int _val, int _pos){ val = _val; p原创 2015-12-11 15:03:05 · 397 阅读 · 0 评论 -
【LeetCode】283 Move Zeroes
题目链接Move Zeroes分析把数组中的0移到数组的末尾,非0的数按照原来的先后顺序排在数组前面。简单题。只要把原来数组中非0的数按照出现的先后顺序进行标记就行了。代码 class Solution { public: void moveZeroes(vector<int>& nums) { int cnt = 0;原创 2015-12-13 18:29:52 · 443 阅读 · 0 评论