classSolution { public: string countAndSay(int n){ if (n==1) return"1";
string res = ""; string r = countAndSay(n-1); char lastC = r[0]; int remainSize = 0; for (auto c : r) { if (lastC==c) { remainSize += 1; } else { res.append(1, (char)(remainSize+'0')); res.append(1, lastC); lastC = c; remainSize = 1; } } res.append(1, (char)(remainSize+'0')); res.append(1, lastC); return res; } };
Result
Success Details
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Count and Say.
Memory Usage: 9.1 MB, less than 38.74% of C++ online submissions for Count and Say.
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. Empty cells are indicated by the character '.'.
A sudoku puzzle...
数独谜题
...and its solution numbers marked in red.
数独完成
Note:
The given board contain only digits 1-9 and the character '.'. You may assume that the given Sudoku puzzle will have a single unique solution. The given board size is always 9x9
“I was under marketing orders to make it look like Java but not make it too big for its britches. It’s just this sort of silly little brother language, right? The sidekick to Java.” -- Brandan Eich (Creator of JavaScript)
local friendinfo = friendmodule.getFriendInfo(friendid) if friendinfo then ui.panelXXX:Show(friendinfo) else friendmodule.queryFriendFromServer(friendid, function()-- callback function friendinfo = friendmodule.getFriendInfo(friendid) ui.panelXXX:Show(friendinfo) end) end
wrapper = { data = {}} wrapper.new = function(id) local instance = { __data={}} setmetatable(instance, {__index = function(table, key) local res = instance.__data[key] if res then return res; end returnrawget(table, key) end}) friendmodule.queryFriendFromServer(id, function(callbackdata)-- callback instance.__data = callbackdata eventsystem.redraw() end) return instance end
friendmodule.getFreindInfo = function(friend) local friendinfo = data[friend] ifnot friendinfo then local res = warpper.new(friend) data[friend] = res return res end return friendinfo end
When we talk about higher-order functions, we mean a function that either: 1.takes one or more functions as arguments, or 2.returns a function as its result
Hi earneet,
We have an easy question that we thought you might be interested in solving:
First Bad Version
Solved 211443 times
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of...
278. First Bad Version
Easy
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 on the previous version, all the versions after a bad version are also bad.
Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.
You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
Example:
Given n = 5, and version = 4 is the first bad version.
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.
本周这篇算法是一个Medium难度的Leetcode题目, 我从之前没有做的题目中随机选了一道题目。无特别原因, 其中的规律考虑了半个小时之后才发现, 中间有事情耽搁,大概从看题目到AC经过了两天。 题目 Next Permutation。
先展示下成果
Success
Details
Runtime: 8 ms, faster than 100.00% of C++ online submissions for Next Permutation.
Memory Usage: 8.6 MB, less than 100.00% of C++ online submissions for Next Permutation.
Description
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place and use only constant extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
Let me make one thing clear first. If you are a beginner programmer, this article is not meant to make you feel bad about the mistakes that you might be making but rather to make you aware of them, teach you to spot signs of them, and remind you to avoid them.
I have made these mistakes in the past and learned from each and every one of them. I am happy to have formed coding habits to help me avoid them. You should do too.
Yes. Planning before jumping into writing code is a good thing, but even good things can hurt you when you do too much of them. Too much water might poison you.
It is often tempting to think beyond the solution that you are writing. All sort of what-ifs will pop into your head with every line of code that you write. This is a good thing for testing edge cases, but it is just wrong to use as a driver for potential needs.