算法
甜甜的闲趣
这个作者很懒,什么都没留下…
展开
-
算法之整数逆转
题目:给定一个 32 位有符号整数,将整数中的数字进行反转。如果反转后的整数溢出,则返回 0。解法一(我自己的沙雕解法)思路:这还是我想了一小时想出来的,真的是。将一个整数按正负分类,然后每次除以10取余放入字符串中,这样输出刚好是反转。关于溢出的问题,给定的是int32,所以可以强制转换成int64,如果两个数相等就说明没有溢出。int转换成string还是要用转换函数的。 //...原创 2018-10-29 20:50:11 · 796 阅读 · 0 评论 -
Find All Numbers Disappeared in an Array
Description:Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this ...原创 2018-12-27 18:32:56 · 163 阅读 · 2 评论 -
Majority Element
Description:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority...原创 2019-01-03 15:36:44 · 126 阅读 · 0 评论 -
19. Remove Nth Node From End of List
Decription:Given a linked list, remove the n-th node from the end of list and return its head.My solution:超时了就是贼蠢,算出链表的长度,得出删除元素的位置,删去元素 ListNode* removeNthFromEnd(ListNode* head, int n) { ...原创 2019-03-04 22:08:14 · 87 阅读 · 0 评论 -
28. Implement strStr()
Description:Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.when needle is an empty string,we will return 0.Solution 1:思路是首先在haystack中的下标最大只...原创 2019-03-01 20:28:22 · 95 阅读 · 0 评论 -
14. Longest Common Prefix
Description:Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string “”.solution1:暴力解法,但是没通过,扎心了。string longestCo...原创 2019-03-03 21:18:22 · 121 阅读 · 0 评论 -
1. Two Sum
Question:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the targ...原创 2019-03-26 15:14:55 · 97 阅读 · 0 评论 -
2. Two Sum II – Input array is sorted
Question:Similar to Question [1. Two Sum], except that the input array is already sorted inascending order.solution1使用两个下标,一个i=0,一个j=nums.size();从后往前计算//做出来还是挺开心的,问题代码可太复杂了,答案是简化了一波,感觉很优秀了vector...原创 2019-03-27 13:08:45 · 157 阅读 · 0 评论 -
4.Valid Palindrome
Description:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.Note:For the purpose of this problem, we define empty string as valid palind...原创 2019-04-05 20:19:56 · 134 阅读 · 0 评论 -
七大排序
1.冒泡排序思想:两两比较,大的放后面,每一回都能得出最大值放在最后,然后开始新一轮。解决:两个for循环,第一个用于控制长度,第二个用于比较时复:O(n*n)代码:class BubbleSort {public: int* bubbleSort(int* A, int n) { // write code here for(int i=n-...原创 2018-12-08 21:27:08 · 254 阅读 · 0 评论 -
算法之709. 转换成小写字母
题目:实现函数 ToLowerCase(),该函数接收一个字符串参数 str,并将该字符串中的大写字母转换成小写字母,之后返回新的字符串。class Solution {public: string toLowerCase(string str) { int n=str.length(); for(int i=0;i<n;i++) ...原创 2018-11-08 20:05:46 · 222 阅读 · 0 评论 -
算法之爬楼梯
题目:假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?沙雕解法一:利用递归,就是最后一步一定是前一步走一步或倒退两步走两步,超时!#include "pch.h"#include <iostream>#include <string>using namespace std;int add(i...原创 2018-10-30 19:56:47 · 1293 阅读 · 0 评论 -
算法之移除元素
题目描述:给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。class Solution {public: int removeElement(vector<int>...原创 2018-11-03 16:09:09 · 228 阅读 · 0 评论 -
★算法之加一
题目:给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。你可以假设除了整数 0 之外,这个整数不会以零开头。我的class Solution {public: vector<int> plusOne(vector<int>& digits) { int n = digits....原创 2018-11-06 16:33:18 · 338 阅读 · 0 评论 -
算法之217. 存在重复元素
题目描述:给定一个整数数组,判断是否存在重复元素。如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。class Solution {public: bool containsDuplicate(vector<int>& nums) { int n=nums.size(); for(in...原创 2018-11-09 11:54:39 · 139 阅读 · 0 评论 -
算法之合并两个有序数组
题目描述:给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。说明:初始化 nums1 和 nums2 的元素数量分别为 m 和 n。你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。我的解法:真的是太沙雕了。class Solution {public: ...原创 2018-11-04 15:50:23 · 4150 阅读 · 2 评论 -
算法之删除排序数组中的重复项
题目:给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。解答:class Solution {public: int removeDuplicates(vector<int>& nums) { int i=1;...原创 2018-11-02 20:38:33 · 160 阅读 · 0 评论 -
算法之53.最大子序和
题目描述:给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。解法一,自己的沙雕解法class Solution {public: int maxSubArray(vector<int>& nums) { int len=num...原创 2018-11-12 22:03:32 · 105 阅读 · 0 评论 -
算法之按奇偶排序数组
题目描述:给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素。答案:class Solution {public: vector<int> sortArrayByParity(vector<int>& A) { int n = A.size(); //数组长度 int j = 0; ...原创 2018-11-05 16:03:36 · 265 阅读 · 0 评论 -
6. Reverse Words in a String
Question:Given an input string s, reverse the string word by word.For example, given s = “the sky is blue”, return “blue is sky the”.solution 1用了一个辅助字符串,然后从后往前遍历原字符串,遍历到空格停止,将刚才的单词放入新字符串,需要两个下标指针来...原创 2019-04-18 17:05:26 · 156 阅读 · 0 评论