1.题目大意Given an array nums, write a function to move all 0‘s to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].Note:You must do this in-place without making a copy of the array.Minimize the total number of operations.解析:给定一个组的数字,把所有0都移到数组的末...
题目:Print a binary tree in an m*n 2D string array following these rules:The row number m should be equal to the height of the given binary tree.The column number n should always be an odd number.The root node‘s value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two part...
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)
Github: https://github.com/illuz/leetcode032. Longest Valid Parentheses (Hard)链接:题目:https://oj.leetcode.com/problems/longest-valid-parentheses/
代码(github):https://github.com/illuz/leetcode题意:问一个字符串里最长的合法括号串的长度。分析:(C++)用栈来做,如果匹配就出栈,然后长度就是 cur - stack_top_pos 也就是 - 匹配的前一个位置。 O(n...
题目描述:你现在是棒球比赛记录员。给定一个字符串列表,每个字符串可以是以下四种类型之一:1.整数(一轮的得分):直接表示您在本轮中获得的积分数。2. "+"(一轮的得分):表示本轮获得的得分是前两轮有效 回合得分的总和。3. "D"(一轮的得分):表示本轮获得的得分是前一轮有效 回合得分的两倍。4. "C"(一个操作,这不是一个回合的分数):表示您获得的最后一个有效 回合的分数是无效的,应该被移除。每一轮的操作都是永久性...
1. 题目1.1 英文题目Given two binary strings a and b, return their sum as a binary string.1.2 中文题目给定两个二进制字符串,返回他们的和(用二进制表示)。输入为非空字符串且只包含数字 1 和 0。1.3输入输出输入输出a = "11", b = "1""100"a = "1010", b = "1011""10101"1.4 约束条件1 <= a.length, b.length <= 104a and b consist only of ‘0‘ or ‘1‘ characters.Each string does not contain leading zeros excep...
class Solution {
public:int firstUniqChar(string s) {map<char, int> a;for(auto c:s){if(!a.count(c)){a[c] = 1;}else{a[c]++;}}for(int i=0;i<s.size();i++){if(a[s[i]]==1){return i;}}return -1;}
};原文:https://www.cnblogs.com/theodoric008/p/9373842.html
Problem:
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would haveexactly one solution. For example, given array S = {-1 2 1 -4}, and target = 1.The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).Solution:此题与15题基本类似,甚至更简单一些,只需要比较和...
class Solution {
public:bool judgeCircle(string moves) {int x = 0;int y = 0;for(auto c:moves){if(c==‘U‘){y++;}else if(c==‘D‘){y--;}else if(c==‘R‘){x++;}else if (c==‘L‘){x--;}}return (x==0 && y==0);}
};
原文:https://www.cnblogs.com/theodoric008/p/9370942.html
Sort a linked list using insertion sort.A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.With each iteration one element (red) is removed from the input data and inserted in-place into the sorted listAlgorithm of Insertion Sort:1Insertion sort iterates, consuming one input element each repetition, and growing a sorted outp...
题目:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[[2],[3,4],[6,5,7],[4,1,8,3]
]
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 =11).Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle....
leetcode 213. 打家劫舍II? 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。? 给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。? 示例:输入: [2, 3, 2]输出: 3解释: ...
题目:
Given an absolute path for a file (Unix-style), simplify it.
For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"click to show corner cases.Corner Cases:Did you consider the case where path = "/../"?
In this case, you should return "/".Another corner case is the path might contain multiple slashes ‘/‘ together,such as "/home//foo/".
In this case, you should ignore redu...
class Solution {
public:int totalNQueens(int n) {//initialize chessboardvector<vector<bool>> boardconf;vector<bool> orig_row;orig_row.assign(n,false);boardconf.assign(n,orig_row);int totalNum = 0;for(int j = 0; j < n; j++){totalNum+= NQueens(boardconf,0,j,n);}return totalNum;}int NQueens(vector<vector<bool>> cb, int i, int j,int n){if(i == n-1){if(not_attack(cb,i,j,n)) return1;elsereturn0;}else{if...
Problem:
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2...
题目这里有 n 个航班,它们分别从 1 到 n 进行编号。有一份航班预订表 bookings ,表中第 i 条预订记录 \(bookings[i] = [first_i, last_i, seats_i]\) 意味着在从 \(first_i\) 到 \(last_i\) (包含 \(first_i\) 和 \(last_i\))的 每个航班 上预订了 \(seats_i\) 个座位。请你返回一个长度为 n 的数组 answer,其中 answer[i] 是航班 i 上预订的座位总数。示例 1:输入:bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
输出:[...